
刪除文件夾保留最新的幾個文件夾的批處理為何執行不了
想實現用它來保持備份數據路徑下保留最新的幾份文件夾,防止自動備份產生的數據無限增多,這段代碼在網上找的,執行不了,本人基礎差,看不出問題所在,請多指教。nclick="copycode($('code0'));">復制代碼
- @Echo Off
- :: User Variables
- :: Set this to the number of folders you want to keep
- Set _NumtoKeep=5
- :: Set this to the folder that contains the folders to check and delete
- Set _Path=D:backup
- If Exist %temp%tf}1{ Del %temp%tf}1{
- PushD %_Path%
- Set _s=%_NumtoKeep%
- If %_NumtoKeep%==1 set _s=single
- Echo Please wait, searching for folders other than the %_s% most recent
- For /F tokens=* skip=%_NumtoKeep% %%I In ('dir %_Path% /AD /B /O-D /TW') Do (
- If Exist %temp%tf}1{ (
- Echo %%I:%%~fI >>%temp%tf}1{
- ) Else (
- Echo.>%temp%tf}1{
- Echo Do you wish to delete the following folders?>>%temp%tf}1{
- Echo Date Name>>%temp%tf}1{
- Echo %%I:%%~fI >>%temp%tf}1{
- ))
- PopD
- If Not Exist %temp%tf}1{ Echo No Folders Found to delete & Goto _Done
- Type %temp%tf}1{ | More
- Set _rdflag= /q
- :_prompt1
- Goto _Removeold
- Set _rdflag=
- :_Removeold
- For /F tokens=1* skip=3 Delims=: %%I In ('type %temp%tf}1{') Do (
- If %_rdflag%== (
- Echo Deleting
- ) Else (
- Echo %%J
- rd/s /q %%J
- ))
- :_Done
- If Exist %temp%tf}1{ (
- echo %temp%tf}1{
- )
- ::Deltree %temp%tf}1{

