标题: [日期时间] 批处理怎么删除指定多少天前的日期格式目录的文件夹? [打印本页]
作者: xiaoyuwxz 时间: 2022-11-28 09:35 标题: 批处理怎么删除指定多少天前的日期格式目录的文件夹?
如在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:55
本帖最后由 hfxiang 于 2022-11-28 09:57 编辑
- forfiles /p "E:\备份文件" /s /m *.* /d -30 /c "cmd /c if @isdir==TRUE rd /s/q @path"
复制代码
作者: xiaoyuwxz 时间: 2022-11-28 15:33
回复 2# hfxiang
谢谢你的指点。
我试了这个命令,它是以目录的修改日期来判断,而删除日期目录内的子目录,在有残留目录的时候,遍历时间很长,而且会显示报错“错误:系统找不到指定的文件。”
测试中还发现这命令还有个缺点,一旦它自动地删除了最下级目录的文件或目录(目录内还有多级子目录和文件的存在),其父目录的修改时间就会改变,继而就不再删除父目录了。命令执行结束后,还有好多目录还是都存在着。
我设想的是直接根据目录的内容(就是日期形式,用日期格式来做运算)来判断,继而直接rd掉这个日期格式目录(最外层的这个目录)
作者: 0475 时间: 2022-11-28 18:56
回复 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"
作者: Batcher 时间: 2022-11-28 19:57
回复 1# xiaoyuwxz
批处理删除指定天数日期之前的文件夹:方案5
http://bbs.bathome.net/thread-3503-1-1.html
作者: xiaoyuwxz 时间: 2022-11-28 23:48
回复 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
请按任意键继续. . .
作者: Batcher 时间: 2022-11-29 10:09
回复 6# xiaoyuwxz
把方案5的第25行
set "FolderDate=!FolderDate:~8,10!"
改成
set "FolderDate=!FolderDate:~0,10!"
作者: hfxiang 时间: 2022-11-29 11:33
回复 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
- popd
复制代码
作者: xiaoyuwxz 时间: 2022-12-2 09:44
本帖最后由 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
- popd
复制代码
使用以上代码进行测试,发现它会遍历当前目录下的所有目录,并把目录中或下层子目录(下下层)的文件或目录删了一部分
备份了当天的,然后测试,
文件备份后 “2022-12-02” 的目录属性
527 MB (552,927,232 字节)
1,273 个文件,204 个文件夹
运行代码后 2022-12-02” 的目录属性
369 MB (386,961,408 字节)
154 个文件,56 个文件夹
也就是说没有进行日期的判断 (备份产生的这个日期文件夹都存在未被删除)
作者: xiaoyuwxz 时间: 2022-12-2 09:54
回复 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
- 请按任意键继续. . .
复制代码
对文件无删除动作
作者: hfxiang 时间: 2022-12-2 11:25
本帖最后由 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
- popd
复制代码
作者: aloha20200628 时间: 2022-12-2 11:46
本帖最后由 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/b
复制代码
作者: aloha20200628 时间: 2022-12-2 12:34
如果不用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/b
复制代码
作者: Batcher 时间: 2022-12-2 13:27
回复 10# xiaoyuwxz
方案5的代码已更新,请再试试。
http://bbs.bathome.net/thread-3503-1-1.html
作者: xiaoyuwxz 时间: 2022-12-2 15:11
回复 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
- 请按任意键继续. . .
复制代码
作者: xiaoyuwxz 时间: 2022-12-2 15:31
回复 13# aloha20200628
谢谢你的回复,我使用你的代码测试了,输入不同的日期后,有时能删除,有时不能删除(就是说有时能删一个,有时能正确全删,有时没反应),输入的日期我是按 “0000-00-00” 的格式输入的。 我把set qd=2022-11-30 这样直接定义后测试没有效果。
作者: xiaoyuwxz 时间: 2022-12-2 15:46
回复 11# hfxiang
谢谢你的回复,这个象是linux上的find命令,它也是按修改时间来计算的。我的本意是要用目录的日期形式来判断而进行删除。不是根据目录的创建或修改时间来删除。当然,用时间来计算可以作为最后一种方法来考虑使用,毕竟原备份出来的目录时间是每天的固定时间。
我也测试了下,还是不准确,会存在误删的情况
作者: aloha20200628 时间: 2022-12-2 17:13
请确认以下三点》
一、目录名须是 20??-??-?? 十位带前导零的格式
二、变量qd的输入值格式也须如上所述,且不能用双引号包裹
三、请用最新的简化版代码
作者: Batcher 时间: 2022-12-2 17:15
回复 15# xiaoyuwxz
试试7楼的方法
读一读方案5代码上面的那段话:
作者: xiaoyuwxz 时间: 2022-12-2 19:30
回复 19# Batcher
完全可以了,谢谢!
作者: hfxiang 时间: 2022-12-3 11:59
回复 17# xiaoyuwxz
最初没有完全理解题意 ,现在知道了 。以下方法应该完全满足要求,只是稍为复杂了些- @echo off & setlocal enabledelayedexpansion
- call :GetDate y m d
- call :DateToDays %y% %m% %d% days1
- @for /f "tokens=1-3 delims=-" %%a in ('dir /b/ad') do (
- call :DateToDays %%a %%b %%c days2
- set/a days=days1-days2
- if !days! GEQ 30 rd /q /s "%%~fa-%%b-%%c"
- )
- endlocal
- exit/b
- :DateToDays %yy% %mm% %dd% days
- setlocal ENABLEEXTENSIONS
- set yy=%1&set mm=%2&set dd=%3
- if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
- set /a dd=100%dd%%%100,mm=100%mm%%%100
- set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
- set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
- endlocal&set %4=%j%&goto :EOF
- :GetDate yyyy mm dd
- setlocal ENABLEEXTENSIONS
- set "first="
- for /f "tokens=1-3 delims=.-/ " %%a in ('date/t') do (
- if not defined first set first=1&set yy=%%a&set mm=%%b&set dd=%%c)
- endlocal&set %1=%yy%&set %2=%mm%&set %3=%dd%&goto :EOF
复制代码
作者: xiaoyuwxz 时间: 2022-12-5 15:43
回复 21# hfxiang
这代码相当好,测试完全正确。太谢谢了!
作者: wanyouxi520w 时间: 2024-6-12 16:20
大佬,楼主这个问题跟我的有些相似,但是代码我不会改。
我的问题是一个路径下,有自动备份的以日期命名的文件夹,比如D:\backup下有06022024 ,06032024,06042024这种日期命名的文件夹,文件夹下是自动备份的文件和子目录。想用批处理自动删除7天前的日期命名的文件夹及子文件夹要怎么改代码呢?
作者: 77七 时间: 2024-6-12 17:23
本帖最后由 77七 于 2024-6-13 10:50 编辑
回复 23# wanyouxi520w
- @echo off
- rem 保存为ansi编码
- rem "http://www.bathome.net/viewthread.php?tid=3330"
- setlocal
- set m=&set d=&set y=
- set DaysAgo=7
- for /f %%i in ('powershell -c "Get-Date (Get-Date).AddDays(-%DaysAgo%) -uformat "%%m%%d%%Y""') do (
- set "DstDate=%%i"
- )
- echo,%DstDate%
- cd /d "D:\backup"
- for /f "delims=" %%i in ('dir /b /ad ^|findstr /rbec:"[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"') do (
- if "%%i" lss "%DstDate%" (
- echo rd /s /q "%%i"
- )
- )
- endlocal
- pause
复制代码
显示正确,则去掉第二个 echo 后再执行
代码错误,参考29楼。
作者: aloha20200628 时间: 2024-6-12 17:39
本帖最后由 aloha20200628 于 2024-6-13 09:46 编辑
回复 23# wanyouxi520w
若楼主备份目录中只有形如 mmddyyyy 日期格式的目录,可用更简化的代码如下...
代码现状是检测版,如检测无误,可删除第3行代码中 echo, 即改为实际删除版。
如删除日期项不包含第7天,可修改代码第3行中 leq 为 lss 即可.
- @echo off &for /f %%d in (' powershell "(get-date).adddays(-7).tostring("""yyyyMMdd""")" ') do (set "days=%%d")
- setlocal enabledelayedexpansion
- for /f "delims=" %%D in ('dir /b/ad ????????') do set "d=%%~nD"&if "!d:~4!!d:~,4!" leq "%days%" (echo,rd /q/s "%%D")
- endlocal&pause&exit/b
复制代码
作者: wanyouxi520w 时间: 2024-6-12 19:05
回复 24# 77七
这个代码验证完全可以删除文件夹,感谢大佬支持。
作者: wanyouxi520w 时间: 2024-6-12 19:06
回复 wanyouxi520w
若楼主备份目录中只有形如 mmddyyyy 日期格式的目录,可用更简化的代码如下...
代 ...
aloha20200628 发表于 2024-6-12 17:39
这个精简代码也可以删除指定日期目录,验证没问题,感谢
作者: newswan 时间: 2024-6-12 23:27
本帖最后由 newswan 于 2024-6-13 11:40 编辑
powershell 比较简单- $sour = "D:\work"
- $dtFormat = "yyyy-MM-dd"
- $days = -7
-
- Push-Location -Paht $sour
-
- Get-ChildItem -Directory | ForEach-Object {
- $dt = [DateTime]::ParseExact( $_.name , $dtFormat , $null )
- if ( $dt -lt (Get-Date).AddDays( $days ) ) {
- Remove-Item -Path $_ -Recurse -Force
- }
- }
-
- Pop-Location
复制代码
作者: WHY 时间: 2024-6-13 08:31
回复 24# 77七
如果考虑跨年份的情况,比如 2024年1月1日,七天前的日期是 2023年12月25日,"12252023" 这个字符串比 "01012024" 还要大。
作者: WHY 时间: 2024-6-13 08:34
- @echo off
- for /f %%i in ('PowerShell "(Get-Date).AddDays(-7).ToString('yyyyMMdd')"') do (
- set "DstDate=%%i"
- )
- setlocal enabledelayedexpansion
- for /f %%i in ('dir /b /ad ^|findstr "^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$"') do (
- set "fd=%%i"
- if "!fd:~4!!fd:~0,4!" LEQ "%DstDate%" (
- echo;rd /s /q "%%i"
- )
- )
- pause
复制代码
作者: aloha20200628 时间: 2024-6-13 09:31
本帖最后由 aloha20200628 于 2024-6-13 09:41 编辑
29楼所言极是,比较 mmddyyyy 格式的日期要排这个雷... 已订正25楼代码。
作者: wanyouxi520w 时间: 2024-6-13 10:31
WHY 发表于 2024-6-13 08:34
这个删除跨年的目录经过验证确实没问题。修补潜在的漏删风险,感谢!
作者: 77七 时间: 2024-6-13 11:03
回复 29# WHY
谢谢大佬指点!第一次遇到 日期在前,年份在后的比较,没有多加思考,我疏忽了。
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |