Board logo

标题: [文件操作] 删除文件夹,保留最新的几个文件夹的批处理为何执行不了? [打印本页]

作者: xjswwh    时间: 2011-10-20 17:26     标题: 删除文件夹,保留最新的几个文件夹的批处理为何执行不了?

  1. @Echo Off
  2. :: User Variables
  3. :: Set this to the number of folders you want to keep
  4. Set _NumtoKeep=5
  5. :: Set this to the folder that contains the folders to check and delete
  6. Set _Path=D:\backup
  7. If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
  8. PushD %_Path%
  9. Set _s=%_NumtoKeep%
  10. If %_NumtoKeep%==1 set _s=single
  11. Echo Please wait, searching for folders other than the %_s% most recent
  12. For /F "tokens=* skip=%_NumtoKeep%" %%I In ('dir "%_Path%" /AD /B /O-D /TW') Do (
  13.    If Exist "%temp%\tf}1{" (
  14.      Echo %%I:%%~fI >>"%temp%\tf}1{"
  15.      ) Else (
  16.      Echo.>"%temp%\tf}1{"
  17.      Echo Do you wish to delete the following folders?>>"%temp%\tf}1{"
  18.      Echo Date       Name>>"%temp%\tf}1{"
  19.      Echo %%I:%%~fI >>"%temp%\tf}1{"
  20.    ))
  21. PopD
  22. If Not Exist "%temp%\tf}1{" Echo No Folders Found to delete & Goto _Done
  23. Type "%temp%\tf}1{" | More
  24. Set _rdflag= /q
  25. :_Prompt1
  26. Goto _Removeold
  27. Set _rdflag=
  28. :_Removeold
  29. For /F "tokens=1* skip=3 Delims=:" %%I In ('type "%temp%\tf}1{"') Do (
  30.   If "%_rdflag%"=="" (
  31.   Echo Deleting
  32. ) Else (
  33.   Echo "%%J"
  34.   rd  /s /q %%J
  35. ))
  36.   
  37. :_Done
  38. If Exist "%temp%\tf}1{" (
  39.   echo "%temp%\tf}1{"
  40. )
  41. ::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