- @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{"
复制代码 想实现用它来保持备份数据路径下保留最新的几份文件夹,防止自动备份产生的数据无限增多,这段代码在网上找的,执行不了,本人基础差,看不出问题所在,请多指教。 |