[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
回复 15# xiaoyuwxz


试试7楼的方法
读一读方案5代码上面的那段话:
根据日期出现在文件夹名称中的具体位置,调整第24行字符串截取。
具体语法请参考参考set命令教程第8页
http://bbs.bathome.net/thread-31727-1-1.html
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

请确认以下三点》
一、目录名须是 20??-??-?? 十位带前导零的格式
二、变量qd的输入值格式也须如上所述,且不能用双引号包裹
三、请用最新的简化版代码

TOP

回复 11# hfxiang


   谢谢你的回复,这个象是linux上的find命令,它也是按修改时间来计算的。我的本意是要用目录的日期形式来判断而进行删除。不是根据目录的创建或修改时间来删除。当然,用时间来计算可以作为最后一种方法来考虑使用,毕竟原备份出来的目录时间是每天的固定时间。
我也测试了下,还是不准确,会存在误删的情况

TOP

回复 13# aloha20200628


    谢谢你的回复,我使用你的代码测试了,输入不同的日期后,有时能删除,有时不能删除(就是说有时能删一个,有时能正确全删,有时没反应),输入的日期我是按 “0000-00-00” 的格式输入的。 我把set qd=2022-11-30 这样直接定义后测试没有效果。

TOP

回复 14# Batcher


    这个方案可以了,不过有个问题,有几个目录是2021-06-23  2021-07-23 2021-07-24 的目录,它不进行删除,其他我设置的目录可以删掉
  1. G:\beifen>rem @echo off
  2. G:\beifen>REM ----------------------------------------------------------------------------------------
  3. G:\beifen>REM 演示:删除指定路径下指定天数之前(以文件夹名中包含的日期字符串为准)的文件夹。
  4. G:\beifen>REM 如果演示结果无误,把rd前面的echo去掉,即可实现真正删除。
  5. G:\beifen>REM 本例假设文件夹名中包含的日期字符串(比如:Archive-2009-12-25)
  6. G:\beifen>REM ----------------------------------------------------------------------------------------
  7. G:\beifen>REM 指定待删除文件夹的存放路径
  8. G:\beifen>set "SrcDir=G:\beifen"
  9. G:\beifen>REM 指定天数
  10. G:\beifen>set "DaysAgo=5"
  11. G:\beifen>echo LastDate=date()-5 1>"C:\Users\ADMINI~1\AppData\Local\Temp\DstDate.vbs"
  12. G:\beifen>echo FmtDate=right(year(LastDate),4) & right("0" & month(LastDate),2) & right("0" & day(LastDate),2) 1>>"C:\Users\ADMINI~1\AppData\Local\Temp\DstDate.vbs"
  13. G:\beifen>echo wscript.echo FmtDate 1>>"C:\Users\ADMINI~1\AppData\Local\Temp\DstDate.vbs"
  14. G:\beifen>for /F %a in ('cscript /nologo "C:\Users\ADMINI~1\AppData\Local\Temp\DstDate.vbs"') do (set "DstDate=%a" )
  15. G:\beifen>(set "DstDate=20221127" )
  16. G:\beifen>set "DstDate=2022-11-27"
  17. G:\beifen>setlocal enabledelayedexpansion
  18. G:\beifen>for / %a in ("G:\beifen\*.*") do (
  19. set "FolderDate=%~na"  
  20. set "FolderDate=!FolderDate:~8,10!"  
  21. if "!FolderDate!" LEQ "2022-11-27" (if exist "%a\" (rd /s /q "%a" ) )
  22. )
  23. G:\beifen>(
  24. set "FolderDate=2021-06-08"  
  25. set "FolderDate=!FolderDate:~8,10!"  
  26. if "!FolderDate!" LEQ "2022-11-27" (if exist "G:\beifen\2021-06-08\" (rd /s /q "G:\beifen\2021-06-08" ) )
  27. )
  28. G:\beifen>(
  29. set "FolderDate=2021-07-23"  
  30. set "FolderDate=!FolderDate:~8,10!"  
  31. if "!FolderDate!" LEQ "2022-11-27" (if exist "G:\beifen\2021-07-23\" (rd /s /q "G:\beifen\2021-07-23" ) )
  32. )
  33. G:\beifen>(
  34. set "FolderDate=2021-07-24"  
  35. set "FolderDate=!FolderDate:~8,10!"  
  36. if "!FolderDate!" LEQ "2022-11-27" (if exist "G:\beifen\2021-07-24\" (rd /s /q "G:\beifen\2021-07-24" ) )
  37. )
  38. G:\beifen>(
  39. set "FolderDate=2021-07-25"  
  40. set "FolderDate=!FolderDate:~8,10!"  
  41. if "!FolderDate!" LEQ "2022-11-27" (if exist "G:\beifen\2021-07-25\" (rd /s /q "G:\beifen\2021-07-25" ) )
  42. )
  43. G:\beifen>(
  44. set "FolderDate=2022-09-08"  
  45. set "FolderDate=!FolderDate:~8,10!"  
  46. if "!FolderDate!" LEQ "2022-11-27" (if exist "G:\beifen\2022-09-08\" (rd /s /q "G:\beifen\2022-09-08" ) )
  47. )
  48. G:\beifen>(
  49. set "FolderDate=2022-11-25"  
  50. set "FolderDate=!FolderDate:~8,10!"  
  51. if "!FolderDate!" LEQ "2022-11-27" (if exist "G:\beifen\2022-11-25\" (rd /s /q "G:\beifen\2022-11-25" ) )
  52. )
  53. G:\beifen>(
  54. set "FolderDate=2022-11-26"  
  55. set "FolderDate=!FolderDate:~8,10!"  
  56. if "!FolderDate!" LEQ "2022-11-27" (if exist "G:\beifen\2022-11-26\" (rd /s /q "G:\beifen\2022-11-26" ) )
  57. )
  58. G:\beifen>(
  59. set "FolderDate=2022-11-27"  
  60. set "FolderDate=!FolderDate:~8,10!"  
  61. if "!FolderDate!" LEQ "2022-11-27" (if exist "G:\beifen\2022-11-27\" (rd /s /q "G:\beifen\2022-11-27" ) )
  62. )
  63. G:\beifen>(
  64. set "FolderDate=2022-11-28"  
  65. set "FolderDate=!FolderDate:~8,10!"  
  66. if "!FolderDate!" LEQ "2022-11-27" (if exist "G:\beifen\2022-11-28\" (rd /s /q "G:\beifen\2022-11-28" ) )
  67. )
  68. G:\beifen>endlocal
  69. G:\beifen>pause
  70. 请按任意键继续. . .
复制代码

TOP

回复 10# xiaoyuwxz


方案5的代码已更新,请再试试。
http://bbs.bathome.net/thread-3503-1-1.html
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

如果不用tc或/tw排序,代码还可简化如下...
  1. @echo off
  2. set "qd=" &set/p "qd=指定目录的删除期限(格式如2022-09-01 早于或等于此限均被删除): "
  3. if not defined qd exit/b
  4. for /f "delims=" %%F in ('dir /b/ad/od 20??-??-??') do (
  5.     if "%%~nF" leq "%qd%" (rd /s /q "%%F")
  6. )
  7. exit/b
复制代码

TOP

本帖最后由 aloha20200628 于 2022-12-2 11:49 编辑

假设当前目录下有多个一级子目录,名如 2022-09-11,2022-10-28,...
指定一个期限,格式如 2022-09-11,其中前导零不能省略,
则如下代码被运行后,会删除早于或等于 2022-09-11 的全部一级子目录。
备注》dir /b/ad/tc/od 参数中的 tc 表示按目录创建日期排序 改为 tw 则表示按目录最后修改日期排序,od 表示升序 o-d 表示降序,本代码须用 od。

楼主可先用模拟样本试用一下。删除目录还须慎重。

  1. @echo off
  2. set/p "qd=指定目录的删除期限(格式如2022-09-01 早于或等于此限均被删除): "
  3. if not defined qd exit/b
  4. setlocal enabledelayedexpansion
  5. for /f "delims=" %%F in ('dir /b/ad/tc/od 20??-??-??') do (
  6.     set "d8=%%~nF"
  7.     if "!d8!" leq "%qd%" (rd /s /q "%%F") else (endlocal &exit/b)
  8. )
  9. endlocal &exit/b
复制代码

TOP

本帖最后由 hfxiang 于 2022-12-2 11:26 编辑

回复 9# xiaoyuwxz


   
如果不介意第3方工具find64.exe( http://bcn.bathome.net/tool/find64.exe
可用如下脚本实现:
  1. @echo off
  2. @rem 保存为ANSI格式
  3. set "SrcDir=G:\文件备份"
  4. set "DaysAgo=30"
  5. pushd "%SrcDir%"
  6. find64 -type f -ctime +%DaysAgo% -delete
  7. find64 -type d -empty -delete
  8. popd
复制代码

TOP

回复 7# Batcher

修改后测试
显示以下代码
  1. G:\beifen>REM ----------------------------------------------------------------------------------------
  2. G:\beifen>REM 演示:删除指定路径下指定天数之前(以文件夹名中包含的日期字符串为准)的文件夹。
  3. G:\beifen>REM 如果演示结果无误,把rd前面的echo去掉,即可实现真正删除。
  4. G:\beifen>REM 本例假设文件夹名中包含的日期字符串(比如:Archive-2009-12-25)
  5. G:\beifen>REM ----------------------------------------------------------------------------------------
  6. G:\beifen>REM 指定待删除文件夹的存放路径
  7. G:\beifen>set SrcDir=G:\beifen
  8. G:\beifen>REM 指定天数
  9. G:\beifen>set DaysAgo=26
  10. G:\beifen>echo LastDate=date()-26 1>"C:\Users\Lenovo\AppData\Local\Temp\DstDate.vbs"
  11. G:\beifen>echo FmtDate=right(year(LastDate),4) & right("0" & month(LastDate),2) & right("0" & day(LastDate),2) 1>>"C:\Users\Lenovo\AppData\Local\Temp\DstDate.vbs"
  12. G:\beifen>echo wscript.echo FmtDate 1>>"C:\Users\Lenovo\AppData\Local\Temp\DstDate.vbs"
  13. G:\beifen>for /F %a in ('cscript /nologo "C:\Users\Lenovo\AppData\Local\Temp\DstDate.vbs"') do (set "DstDate=%a" )
  14. G:\beifen>(set "DstDate=20221106" )
  15. G:\beifen>set DstDate=2022-11-06
  16. G:\beifen>setlocal enabledelayedexpansion
  17. G:\beifen>for / %a in ("G:\beifen\*.*") do (if "%~ta" LEQ "2022-11-06" (
  18. set "FolderDate=%~na"  
  19. set "FolderDate=!FolderDate:~0,10!"  
  20. if "!FolderDate!" LEQ "2022-11-06" (if exist "%a\" (echo rd /s /q "%a" ) )
  21. ) )
  22. G:\beifen>(if "2022-12-02 09:31" LEQ "2022-11-06" (
  23. set "FolderDate=2022-10-31"  
  24. set "FolderDate=!FolderDate:~0,10!"  
  25. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-10-31\" (echo rd /s /q "G:\beifen\2022-10-31" ) )
  26. ) )
  27. G:\beifen>(if "2022-12-02 09:31" LEQ "2022-11-06" (
  28. set "FolderDate=2022-11-01"  
  29. set "FolderDate=!FolderDate:~0,10!"  
  30. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-01\" (echo rd /s /q "G:\beifen\2022-11-01" ) )
  31. ) )
  32. G:\beifen>(if "2022-12-02 09:31" LEQ "2022-11-06" (
  33. set "FolderDate=2022-11-02"  
  34. set "FolderDate=!FolderDate:~0,10!"  
  35. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-02\" (echo rd /s /q "G:\beifen\2022-11-02" ) )
  36. ) )
  37. G:\beifen>(if "2022-12-02 09:31" LEQ "2022-11-06" (
  38. set "FolderDate=2022-11-03"  
  39. set "FolderDate=!FolderDate:~0,10!"  
  40. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-03\" (echo rd /s /q "G:\beifen\2022-11-03" ) )
  41. ) )
  42. G:\beifen>(if "2022-12-02 09:31" LEQ "2022-11-06" (
  43. set "FolderDate=2022-11-04"  
  44. set "FolderDate=!FolderDate:~0,10!"  
  45. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-04\" (echo rd /s /q "G:\beifen\2022-11-04" ) )
  46. ) )
  47. G:\beifen>(if "2022-12-02 09:31" LEQ "2022-11-06" (
  48. set "FolderDate=2022-11-07"  
  49. set "FolderDate=!FolderDate:~0,10!"  
  50. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-07\" (echo rd /s /q "G:\beifen\2022-11-07" ) )
  51. ) )
  52. G:\beifen>(if "2022-12-02 09:31" LEQ "2022-11-06" (
  53. set "FolderDate=2022-11-08"  
  54. set "FolderDate=!FolderDate:~0,10!"  
  55. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-08\" (echo rd /s /q "G:\beifen\2022-11-08" ) )
  56. ) )
  57. G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
  58. set "FolderDate=2022-11-09"  
  59. set "FolderDate=!FolderDate:~0,10!"  
  60. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-09\" (echo rd /s /q "G:\beifen\2022-11-09" ) )
  61. ) )
  62. G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
  63. set "FolderDate=2022-11-10"  
  64. set "FolderDate=!FolderDate:~0,10!"  
  65. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-10\" (echo rd /s /q "G:\beifen\2022-11-10" ) )
  66. ) )
  67. G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
  68. set "FolderDate=2022-11-11"  
  69. set "FolderDate=!FolderDate:~0,10!"  
  70. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-11\" (echo rd /s /q "G:\beifen\2022-11-11" ) )
  71. ) )
  72. G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
  73. set "FolderDate=2022-11-14"  
  74. set "FolderDate=!FolderDate:~0,10!"  
  75. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-14\" (echo rd /s /q "G:\beifen\2022-11-14" ) )
  76. ) )
  77. G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
  78. set "FolderDate=2022-11-15"  
  79. set "FolderDate=!FolderDate:~0,10!"  
  80. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-15\" (echo rd /s /q "G:\beifen\2022-11-15" ) )
  81. ) )
  82. G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
  83. set "FolderDate=2022-11-16"  
  84. set "FolderDate=!FolderDate:~0,10!"  
  85. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-16\" (echo rd /s /q "G:\beifen\2022-11-16" ) )
  86. ) )
  87. G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
  88. set "FolderDate=2022-11-17"  
  89. set "FolderDate=!FolderDate:~0,10!"  
  90. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-17\" (echo rd /s /q "G:\beifen\2022-11-17" ) )
  91. ) )
  92. G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
  93. set "FolderDate=2022-11-18"  
  94. set "FolderDate=!FolderDate:~0,10!"  
  95. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-18\" (echo rd /s /q "G:\beifen\2022-11-18" ) )
  96. ) )
  97. G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
  98. set "FolderDate=2022-11-21"  
  99. set "FolderDate=!FolderDate:~0,10!"  
  100. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-21\" (echo rd /s /q "G:\beifen\2022-11-21" ) )
  101. ) )
  102. G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
  103. set "FolderDate=2022-11-22"  
  104. set "FolderDate=!FolderDate:~0,10!"  
  105. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-22\" (echo rd /s /q "G:\beifen\2022-11-22" ) )
  106. ) )
  107. G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
  108. set "FolderDate=2022-11-23"  
  109. set "FolderDate=!FolderDate:~0,10!"  
  110. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-23\" (echo rd /s /q "G:\beifen\2022-11-23" ) )
  111. ) )
  112. G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
  113. set "FolderDate=2022-11-24"  
  114. set "FolderDate=!FolderDate:~0,10!"  
  115. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-24\" (echo rd /s /q "G:\beifen\2022-11-24" ) )
  116. ) )
  117. G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
  118. set "FolderDate=2022-11-25"  
  119. set "FolderDate=!FolderDate:~0,10!"  
  120. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-25\" (echo rd /s /q "G:\beifen\2022-11-25" ) )
  121. ) )
  122. G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
  123. set "FolderDate=2022-11-28"  
  124. set "FolderDate=!FolderDate:~0,10!"  
  125. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-28\" (echo rd /s /q "G:\beifen\2022-11-28" ) )
  126. ) )
  127. G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
  128. set "FolderDate=2022-11-29"  
  129. set "FolderDate=!FolderDate:~0,10!"  
  130. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-29\" (echo rd /s /q "G:\beifen\2022-11-29" ) )
  131. ) )
  132. G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
  133. set "FolderDate=2022-11-30"  
  134. set "FolderDate=!FolderDate:~0,10!"  
  135. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-30\" (echo rd /s /q "G:\beifen\2022-11-30" ) )
  136. ) )
  137. G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
  138. set "FolderDate=2022-12-01"  
  139. set "FolderDate=!FolderDate:~0,10!"  
  140. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-12-01\" (echo rd /s /q "G:\beifen\2022-12-01" ) )
  141. ) )
  142. G:\beifen>(if "2022-12-02 09:46" LEQ "2022-11-06" (
  143. set "FolderDate=2022-12-02"  
  144. set "FolderDate=!FolderDate:~0,10!"  
  145. if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-12-02\" (echo rd /s /q "G:\beifen\2022-12-02" ) )
  146. ) )
  147. G:\beifen>endlocal
  148. G:\beifen>pause
  149. 请按任意键继续. . .
复制代码
对文件无删除动作

TOP

本帖最后由 xiaoyuwxz 于 2022-12-2 09:49 编辑

回复 8# hfxiang
  1. @rem 保存为ANSI格式
  2. set "SrcDir=G:\文件备份"
  3. set "DaysAgo=30"
  4. pushd "%SrcDir%"
  5. forfiles /p "%SrcDir%" /s /m *.* /d -%DaysAgo% /c "cmd /c if @isdir==FALSE del @path"
  6. setlocal enabledelayedexpansion
  7. for /f "delims=" %%a in ('dir/b/s/ad^|sort/r') do (
  8. set "Empty_Folder="
  9. for /f "delims=" %%b in ('dir/a/b "%%~a"') do set "Empty_Folder=%%~b"
  10. if "!Empty_Folder!." equ "." rd/q/s "%%~a">nul 2>&1
  11. )
  12. endlocal
  13. popd
复制代码
使用以上代码进行测试,发现它会遍历当前目录下的所有目录,并把目录中或下层子目录(下下层)的文件或目录删了一部分
备份了当天的,然后测试,

文件备份后 “2022-12-02” 的目录属性
527 MB (552,927,232 字节)
1,273 个文件,204 个文件夹

运行代码后 2022-12-02” 的目录属性
369 MB (386,961,408 字节)
154 个文件,56 个文件夹

也就是说没有进行日期的判断  (备份产生的这个日期文件夹都存在未被删除)

TOP

回复 4# 0475


   
修订后的脚本如下,请测试
  1. @echo off
  2. @rem 保存为ANSI格式
  3. set "SrcDir=E:\0006\222"
  4. set "DaysAgo=7"
  5. pushd "%SrcDir%"
  6. forfiles /p "%SrcDir%" /s /m *.* /d -%DaysAgo% /c "cmd /c if @isdir==FALSE del @path" >nul 2>&1
  7. setlocal enabledelayedexpansion
  8. for /f "delims=" %%a in ('dir/b/s/ad^|sort/r') do (
  9. set "Empty_Folder="
  10. for /f "delims=" %%b in ('dir/a/b "%%~a"') do set "Empty_Folder=%%~b"
  11. if "!Empty_Folder!." equ "." rd/q/s "%%~a">nul 2>&1
  12. )
  13. endlocal
  14. popd
复制代码

TOP

回复 6# xiaoyuwxz


把方案5的第25行
set "FolderDate=!FolderDate:~8,10!"
改成
set "FolderDate=!FolderDate:~0,10!"
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

回复 5# Batcher


   我使用了第五个方案的代码,没有动作,请帮忙分析下原因。

现目录下的文件如下:
G:\beifen 的目录

2022-11-28  23:46    <DIR>          .
2022-11-28  23:46    <DIR>          ..
2022-11-28  23:08    <DIR>          2018-07-08
2022-11-28  23:08    <DIR>          2018-07-10
2022-11-28  23:08    <DIR>          2018-07-17
2022-11-28  23:07    <DIR>          2021-07-23
2022-11-28  23:08    <DIR>          2022-07-06
2022-11-28  23:07    <DIR>          2022-11-25
2022-11-28  23:07    <DIR>          2022-11-26
2022-11-28  23:07    <DIR>          2022-11-27
2022-11-28  23:07    <DIR>          2022-11-28
2022-11-28  23:46             3,651 a.txt
2022-11-28  23:46                 0 b.txt
2021-07-23  10:40               264 backup.log
2018-07-17  11:57             1,431 beifen - 副本.bat
2018-07-17  10:58             1,432 beifen.bat
2022-11-28  23:38             1,175 ddeell.bat

生成的DstDate.vbs 内容为:
LastDate=date()-3
FmtDate=right(year(LastDate),4) & right("0" & month(LastDate),2) & right("0" & day(LastDate),2)
wscript.echo FmtDate

运行ddeell.bat得到 的内容为:


G:\beifen>REM ----------------------------------------------------------------------------------------

G:\beifen>REM 演示:删除指定路径下指定天数之前(以文件夹名中包含的日期字符串为准)的文件夹。

G:\beifen>REM 如果演示结果无误,把rd前面的echo去掉,即可实现真正删除。

G:\beifen>REM 本例假设文件夹名中包含的日期字符串(比如:Archive-2009-12-25)

G:\beifen>REM ----------------------------------------------------------------------------------------

G:\beifen>REM 指定待删除文件夹的存放路径

G:\beifen>set SrcDir=G:\beifen

G:\beifen>REM 指定天数

G:\beifen>set DaysAgo=3

G:\beifen>echo LastDate=date()-3 1>"C:\Users\ADMINI~1\AppData\Local\Temp\DstDate.vbs"

G:\beifen>echo FmtDate=right(year(LastDate),4) & right("0" & month(LastDate),2) & right("0" & day(LastDate),2) 1>>"C:\Users\ADMINI~1\AppData\Local\Temp\DstDate.vbs"

G:\beifen>echo wscript.echo FmtDate 1>>"C:\Users\ADMINI~1\AppData\Local\Temp\DstDate.vbs"

G:\beifen>for /F %a in ('cscript /nologo "C:\Users\ADMINI~1\AppData\Local\Temp\DstDate.vbs"') do (set "DstDate=%a" )

G:\beifen>(set "DstDate=20221125" )

G:\beifen>set DstDate=2022-11-25

G:\beifen>setlocal enabledelayedexpansion

G:\beifen>for / %a in ("G:\beifen\*.*") do (if "%~ta" LEQ "2022-11-25" (
set "FolderDate=%~na"  
set "FolderDate=!FolderDate:~8,10!"  
if "!FolderDate!" LEQ "2022-11-25" (if exist "%a\" (rd /s /q "%a" ) )
) )

G:\beifen>(if "2022-11-28 23:08" LEQ "2022-11-25" (
set "FolderDate=2018-07-08"  
set "FolderDate=!FolderDate:~8,10!"  
if "!FolderDate!" LEQ "2022-11-25" (if exist "G:\beifen\2018-07-08\" (rd /s /q "G:\beifen\2018-07-08" ) )
) )

G:\beifen>(if "2022-11-28 23:08" LEQ "2022-11-25" (
set "FolderDate=2018-07-10"  
set "FolderDate=!FolderDate:~8,10!"  
if "!FolderDate!" LEQ "2022-11-25" (if exist "G:\beifen\2018-07-10\" (rd /s /q "G:\beifen\2018-07-10" ) )
) )

G:\beifen>(if "2022-11-28 23:08" LEQ "2022-11-25" (
set "FolderDate=2018-07-17"  
set "FolderDate=!FolderDate:~8,10!"  
if "!FolderDate!" LEQ "2022-11-25" (if exist "G:\beifen\2018-07-17\" (rd /s /q "G:\beifen\2018-07-17" ) )
) )

G:\beifen>(if "2022-11-28 23:07" LEQ "2022-11-25" (
set "FolderDate=2021-07-23"  
set "FolderDate=!FolderDate:~8,10!"  
if "!FolderDate!" LEQ "2022-11-25" (if exist "G:\beifen\2021-07-23\" (rd /s /q "G:\beifen\2021-07-23" ) )
) )

G:\beifen>(if "2022-11-28 23:08" LEQ "2022-11-25" (
set "FolderDate=2022-07-06"  
set "FolderDate=!FolderDate:~8,10!"  
if "!FolderDate!" LEQ "2022-11-25" (if exist "G:\beifen\2022-07-06\" (rd /s /q "G:\beifen\2022-07-06" ) )
) )

G:\beifen>(if "2022-11-28 23:07" LEQ "2022-11-25" (
set "FolderDate=2022-11-25"  
set "FolderDate=!FolderDate:~8,10!"  
if "!FolderDate!" LEQ "2022-11-25" (if exist "G:\beifen\2022-11-25\" (rd /s /q "G:\beifen\2022-11-25" ) )
) )

G:\beifen>(if "2022-11-28 23:07" LEQ "2022-11-25" (
set "FolderDate=2022-11-26"  
set "FolderDate=!FolderDate:~8,10!"  
if "!FolderDate!" LEQ "2022-11-25" (if exist "G:\beifen\2022-11-26\" (rd /s /q "G:\beifen\2022-11-26" ) )
) )

G:\beifen>(if "2022-11-28 23:07" LEQ "2022-11-25" (
set "FolderDate=2022-11-27"  
set "FolderDate=!FolderDate:~8,10!"  
if "!FolderDate!" LEQ "2022-11-25" (if exist "G:\beifen\2022-11-27\" (rd /s /q "G:\beifen\2022-11-27" ) )
) )

G:\beifen>(if "2022-11-28 23:07" LEQ "2022-11-25" (
set "FolderDate=2022-11-28"  
set "FolderDate=!FolderDate:~8,10!"  
if "!FolderDate!" LEQ "2022-11-25" (if exist "G:\beifen\2022-11-28\" (rd /s /q "G:\beifen\2022-11-28" ) )
) )

G:\beifen>endlocal

G:\beifen>pause
请按任意键继续. . .

TOP

回复 1# xiaoyuwxz


批处理删除指定天数日期之前的文件夹:方案5
http://bbs.bathome.net/thread-3503-1-1.html
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

返回列表