返回列表 发帖

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

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

本帖最后由 hfxiang 于 2022-11-28 09:57 编辑
forfiles /p "E:\备份文件" /s /m *.* /d -30 /c "cmd /c if @isdir==TRUE rd /s/q @path"COPY

TOP

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

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

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

TOP

回复  hfxiang
谢谢你的指点。

我试了这个命令,它是以目录的修改日期来判断,而删除日期目录内的子目 ...
xiaoyuwxz 发表于 2022-11-28 15:33



rem 下面为只删除指定目录下的子文件夹和文件,不删除本身目录 ,文件夹名称后面不要添加 \ 字符
set "SrcDir=E:\0006\222"
rem 下面为指定天数之前
set DaysAgo=7

::说明:彻底删除指定路径下指定天数之前(以文件的最后修改日期为准)的文件夹和文件。是彻底删除,并不会出现在回收站里。
forfiles /p %SrcDir% /s /m "*" /d -%DaysAgo% /c "cmd /c del /f /q @path & RD /S /Q @path"

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

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

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

回复 4# 0475


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

TOP

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

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

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

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

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

TOP

回复 7# Batcher

修改后测试
显示以下代码
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=26
G:\beifen>echo LastDate=date()-26 1>"C:\Users\Lenovo\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\Lenovo\AppData\Local\Temp\DstDate.vbs"
G:\beifen>echo wscript.echo FmtDate 1>>"C:\Users\Lenovo\AppData\Local\Temp\DstDate.vbs"
G:\beifen>for /F %a in ('cscript /nologo "C:\Users\Lenovo\AppData\Local\Temp\DstDate.vbs"') do (set "DstDate=%a" )
G:\beifen>(set "DstDate=20221106" )
G:\beifen>set DstDate=2022-11-06
G:\beifen>setlocal enabledelayedexpansion
G:\beifen>for / %a in ("G:\beifen\*.*") do (if "%~ta" LEQ "2022-11-06" (
set "FolderDate=%~na"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "%a\" (echo rd /s /q "%a" ) )
) )
G:\beifen>(if "2022-12-02 09:31" LEQ "2022-11-06" (
set "FolderDate=2022-10-31"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-10-31\" (echo rd /s /q "G:\beifen\2022-10-31" ) )
) )
G:\beifen>(if "2022-12-02 09:31" LEQ "2022-11-06" (
set "FolderDate=2022-11-01"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-01\" (echo rd /s /q "G:\beifen\2022-11-01" ) )
) )
G:\beifen>(if "2022-12-02 09:31" LEQ "2022-11-06" (
set "FolderDate=2022-11-02"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-02\" (echo rd /s /q "G:\beifen\2022-11-02" ) )
) )
G:\beifen>(if "2022-12-02 09:31" LEQ "2022-11-06" (
set "FolderDate=2022-11-03"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-03\" (echo rd /s /q "G:\beifen\2022-11-03" ) )
) )
G:\beifen>(if "2022-12-02 09:31" LEQ "2022-11-06" (
set "FolderDate=2022-11-04"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-04\" (echo rd /s /q "G:\beifen\2022-11-04" ) )
) )
G:\beifen>(if "2022-12-02 09:31" LEQ "2022-11-06" (
set "FolderDate=2022-11-07"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-07\" (echo rd /s /q "G:\beifen\2022-11-07" ) )
) )
G:\beifen>(if "2022-12-02 09:31" LEQ "2022-11-06" (
set "FolderDate=2022-11-08"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-08\" (echo rd /s /q "G:\beifen\2022-11-08" ) )
) )
G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
set "FolderDate=2022-11-09"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-09\" (echo rd /s /q "G:\beifen\2022-11-09" ) )
) )
G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
set "FolderDate=2022-11-10"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-10\" (echo rd /s /q "G:\beifen\2022-11-10" ) )
) )
G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
set "FolderDate=2022-11-11"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-11\" (echo rd /s /q "G:\beifen\2022-11-11" ) )
) )
G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
set "FolderDate=2022-11-14"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-14\" (echo rd /s /q "G:\beifen\2022-11-14" ) )
) )
G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
set "FolderDate=2022-11-15"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-15\" (echo rd /s /q "G:\beifen\2022-11-15" ) )
) )
G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
set "FolderDate=2022-11-16"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-16\" (echo rd /s /q "G:\beifen\2022-11-16" ) )
) )
G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
set "FolderDate=2022-11-17"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-17\" (echo rd /s /q "G:\beifen\2022-11-17" ) )
) )
G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
set "FolderDate=2022-11-18"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-18\" (echo rd /s /q "G:\beifen\2022-11-18" ) )
) )
G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
set "FolderDate=2022-11-21"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-21\" (echo rd /s /q "G:\beifen\2022-11-21" ) )
) )
G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
set "FolderDate=2022-11-22"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-22\" (echo rd /s /q "G:\beifen\2022-11-22" ) )
) )
G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
set "FolderDate=2022-11-23"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-23\" (echo rd /s /q "G:\beifen\2022-11-23" ) )
) )
G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
set "FolderDate=2022-11-24"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-24\" (echo rd /s /q "G:\beifen\2022-11-24" ) )
) )
G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
set "FolderDate=2022-11-25"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-25\" (echo rd /s /q "G:\beifen\2022-11-25" ) )
) )
G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
set "FolderDate=2022-11-28"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-28\" (echo rd /s /q "G:\beifen\2022-11-28" ) )
) )
G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
set "FolderDate=2022-11-29"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-29\" (echo rd /s /q "G:\beifen\2022-11-29" ) )
) )
G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
set "FolderDate=2022-11-30"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-11-30\" (echo rd /s /q "G:\beifen\2022-11-30" ) )
) )
G:\beifen>(if "2022-12-02 09:02" LEQ "2022-11-06" (
set "FolderDate=2022-12-01"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-12-01\" (echo rd /s /q "G:\beifen\2022-12-01" ) )
) )
G:\beifen>(if "2022-12-02 09:46" LEQ "2022-11-06" (
set "FolderDate=2022-12-02"  
set "FolderDate=!FolderDate:~0,10!"  
if "!FolderDate!" LEQ "2022-11-06" (if exist "G:\beifen\2022-12-02\" (echo rd /s /q "G:\beifen\2022-12-02" ) )
) )
G:\beifen>endlocal
G:\beifen>pause
请按任意键继续. . . COPY
对文件无删除动作

TOP

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

回复 9# xiaoyuwxz


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

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。

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

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

TOP

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

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

回复 14# Batcher


    这个方案可以了,不过有个问题,有几个目录是2021-06-23  2021-07-23 2021-07-24 的目录,它不进行删除,其他我设置的目录可以删掉
G:\beifen>rem @echo off
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=5"
G:\beifen>echo LastDate=date()-5 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=20221127" )
G:\beifen>set "DstDate=2022-11-27"
G:\beifen>setlocal enabledelayedexpansion
G:\beifen>for / %a in ("G:\beifen\*.*") do (
set "FolderDate=%~na"  
set "FolderDate=!FolderDate:~8,10!"  
if "!FolderDate!" LEQ "2022-11-27" (if exist "%a\" (rd /s /q "%a" ) )
)
G:\beifen>(
set "FolderDate=2021-06-08"  
set "FolderDate=!FolderDate:~8,10!"  
if "!FolderDate!" LEQ "2022-11-27" (if exist "G:\beifen\2021-06-08\" (rd /s /q "G:\beifen\2021-06-08" ) )
)
G:\beifen>(
set "FolderDate=2021-07-23"  
set "FolderDate=!FolderDate:~8,10!"  
if "!FolderDate!" LEQ "2022-11-27" (if exist "G:\beifen\2021-07-23\" (rd /s /q "G:\beifen\2021-07-23" ) )
)
G:\beifen>(
set "FolderDate=2021-07-24"  
set "FolderDate=!FolderDate:~8,10!"  
if "!FolderDate!" LEQ "2022-11-27" (if exist "G:\beifen\2021-07-24\" (rd /s /q "G:\beifen\2021-07-24" ) )
)
G:\beifen>(
set "FolderDate=2021-07-25"  
set "FolderDate=!FolderDate:~8,10!"  
if "!FolderDate!" LEQ "2022-11-27" (if exist "G:\beifen\2021-07-25\" (rd /s /q "G:\beifen\2021-07-25" ) )
)
G:\beifen>(
set "FolderDate=2022-09-08"  
set "FolderDate=!FolderDate:~8,10!"  
if "!FolderDate!" LEQ "2022-11-27" (if exist "G:\beifen\2022-09-08\" (rd /s /q "G:\beifen\2022-09-08" ) )
)
G:\beifen>(
set "FolderDate=2022-11-25"  
set "FolderDate=!FolderDate:~8,10!"  
if "!FolderDate!" LEQ "2022-11-27" (if exist "G:\beifen\2022-11-25\" (rd /s /q "G:\beifen\2022-11-25" ) )
)
G:\beifen>(
set "FolderDate=2022-11-26"  
set "FolderDate=!FolderDate:~8,10!"  
if "!FolderDate!" LEQ "2022-11-27" (if exist "G:\beifen\2022-11-26\" (rd /s /q "G:\beifen\2022-11-26" ) )
)
G:\beifen>(
set "FolderDate=2022-11-27"  
set "FolderDate=!FolderDate:~8,10!"  
if "!FolderDate!" LEQ "2022-11-27" (if exist "G:\beifen\2022-11-27\" (rd /s /q "G:\beifen\2022-11-27" ) )
)
G:\beifen>(
set "FolderDate=2022-11-28"  
set "FolderDate=!FolderDate:~8,10!"  
if "!FolderDate!" LEQ "2022-11-27" (if exist "G:\beifen\2022-11-28\" (rd /s /q "G:\beifen\2022-11-28" ) )
)
G:\beifen>endlocal
G:\beifen>pause
请按任意键继续. . . COPY

TOP

返回列表