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

[日期时间] 批处理怎么删除指定多少天前的日期格式目录的文件夹?

如在E:\备份文件\  下有很多时间格式的目录   2022-08-12 2022-09-10 2022-09-11 2022-10-28 2022-10-29  这些目录是通过计划任务每天备份文件生成的,现在想删除30天以前的目录 ,也就是说要删除 2022-10-29以前的所有目录,怎么写批处理。
原先做的是每次在备份后自动删除前30天那一天的备份,那是因为电脑天天开着(给服务器备份mysql数据库用),可以删除30天前每天的备份,但是用来备份工作文件的话,电脑不是每天开着的话,会导到前边的目录会遗留下来,而不能自动删除掉,需手工再去删。

回复 2# hfxiang
谢谢你的指点。

我试了这个命令,它是以目录的修改日期来判断,而删除日期目录内的子目录,在有残留目录的时候,遍历时间很长,而且会显示报错“错误:系统找不到指定的文件。”
测试中还发现这命令还有个缺点,一旦它自动地删除了最下级目录的文件或目录(目录内还有多级子目录和文件的存在),其父目录的修改时间就会改变,继而就不再删除父目录了。命令执行结束后,还有好多目录还是都存在着。

我设想的是直接根据目录的内容(就是日期形式,用日期格式来做运算)来判断,继而直接rd掉这个日期格式目录(最外层的这个目录)

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

本帖最后由 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

回复 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

回复 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

回复 13# aloha20200628


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

TOP

回复 11# hfxiang


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

TOP

回复 19# Batcher


    完全可以了,谢谢!

TOP

回复 21# hfxiang


    这代码相当好,测试完全正确。太谢谢了!

TOP

返回列表