标题: [文件操作] 删除文件夹,保留最新的几个文件夹的批处理为何执行不了? [打印本页]
作者: xjswwh 时间: 2011-10-20 17:26 标题: 删除文件夹,保留最新的几个文件夹的批处理为何执行不了?
- @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{"
复制代码
想实现用它来保持备份数据路径下保留最新的几份文件夹,防止自动备份产生的数据无限增多,这段代码在网上找的,执行不了,本人基础差,看不出问题所在,请多指教。
作者: Batcher 时间: 2011-10-20 23:02
你把@echo off删掉,看看哪一步出问题了。
作者: xjswwh 时间: 2011-10-21 09:03
我把那个 echo off去掉了,还是得不到有用的提示信息,执行脚本一闪就过了,我在最后加了个pause,但是,前面的信息都是代码,没有代码问题的提示信息。
作者: Batcher 时间: 2011-10-21 09:35
回复 3# xjswwh
就把那个执行过程贴出来,大家帮你找问题。
作者: xjswwh 时间: 2011-10-21 10:06
在代码最后一行加入了一个pause得到的执行结果信息如下:
D:\backup>Echo on
D:\backup>Set _NumtoKeep=5
D:\backup>Set _Path="D:\backup"
D:\backup>If Exist "C:\Users\Wangh\AppData\Local\Temp\tf}1{" Del "C:\Users\Wangh
\AppData\Local\Temp\tf}1{"
D:\backup>PUSHD "D:\backup"
D:\backup>Set _s=5
D:\backup>If 5 == 1 set _s=single
D:\backup>Echo Please wait, searching for folders other than the 5 most recent
Please wait, searching for folders other than the 5 most recent
D:\backup>For /F "tokens=7 skip=5" %I In ('dir ""D:\backup"" /AD /B /O-D /TW') D
o (If Exist "C:\Users\Wangh\AppData\Local\Temp\tf}1{" (Echo %I:%~fI 1>>"C:\User
s\Wangh\AppData\Local\Temp\tf}1{" ) Else (
Echo 1>"C:\Users\Wangh\AppData\Local\Temp\tf}1{"
Echo Do you wish to delete the following folders? 1>>"C:\Users\Wangh\AppData\L
ocal\Temp\tf}1{"
Echo Date Name 1>>"C:\Users\Wangh\AppData\Local\Temp\tf}1{"
Echo %I:%~fI 1>>"C:\Users\Wangh\AppData\Local\Temp\tf}1{"
) )
D:\backup>POPD
D:\backup>If Not Exist "C:\Users\Wangh\AppData\Local\Temp\tf}1{" Echo No Folders
Found to delete & Goto _Done
No Folders Found to delete
D:\backup>If Exist "C:\Users\Wangh\AppData\Local\Temp\tf}1{" (echo "C:\Users\Wan
gh\AppData\Local\Temp\tf}1{" )
D:\backup>pause
Press any key to continue . . .
作者: xjswwh 时间: 2011-10-22 09:16
期待您的指教,谢谢!
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |