【方案一】BAT + REG + VBS
复制代码 代码如下:
@echo off
::演示:删除指定路径下指定天数之前(以文件夹的最后修改日期为准)的文件夹。
::如果演示结果无误,把rd前面的echo去掉,即可实现真正删除。
::本例调用了临时VBS代码进行日期计算
::本例为兼容不同的日期格式,调用reg命令(XP系统自带)统一设置日期格式,
::处理完毕之后再把日期格式恢复成原来的状态。
rem 指定待删除文件夹的存放路径
set SrcDir=C:/Test
rem 指定天数
set DaysAgo=1
for /f "skip=2 delims=" %%a in ('reg query "HKEY_CURRENT_USER/Control Panel/International" /v sShortDate') do (
set "RegDateOld=%%a"
)
set RegDateOld=%RegDateOld:~-8%
reg add "HKEY_CURRENT_USER/Control Panel/International" /v sShortDate /t REG_SZ /d yyyy-M-d /f>nul
>"%temp%/DstDate.vbs" echo LastDate=date()-%DaysAgo%
"%temp%/DstDate.vbs" echo FmtDate=right(year(LastDate),4) ^& right("0" ^& month(LastDate),2) ^& right("0" ^& day(LastDate),2)
"%temp%/DstDate.vbs" echo wscript.echo FmtDate
for /f %%a in ('cscript /nologo "%temp%/DstDate.vbs"') do (
set "DstDate=%%a"
)
set DstDate=%DstDate:~0,4%-%DstDate:~4,2%-%DstDate:~6,2%
for /d %%a in ("%SrcDir%/*.*") do (
if "%%~ta" leq "%DstDate%" (
if exist "%%a/"
echo rd /s /q "%%a"
)
)
)
reg add "HKEY_CURRENT_USER/Control Panel/International" /v sShortDate /t REG_SZ /d %RegDateOld% /f>nul
pause
【方案二】BAT + REG + Ritchie Lawrence 日期函数
复制代码 代码如下:
@echo off
::演示:删除指定路径下指定天数之前(以文件夹的最后修改日期为准)的文件夹。
::如果演示结果无误,把rd前面的echo去掉,即可实现真正删除。
::本例调用了 Ritchie Lawrence 的日期函数进行日期计算
::日期转换的核心算法请参考http://bbs.bathome.cn/thread-3056-1-1.html
::本例为兼容不同的日期格式,调用reg命令(XP系统自带)统一设置日期格式,
::处理完毕之后再把日期格式恢复成原来的状态。
rem 指定待删除文件夹的存放路径
set SrcDir=C:/Test
rem 指定天数
set DaysAgo=1
for /f "skip=2 delims=" %%a in ('reg query "HKEY_CURRENT_USER/Control Panel/International" /v sShortDate') do (
set "RegDateOld=%%a"
)
set RegDateOld=%RegDateOld:~-8%
reg add "HKEY_CURRENT_USER/Control Panel/International" /v sShortDate /t REG_SZ /d yyyy-M-d /f>nul
call :DateToDays %date:~0,4% %date:~5,2% %date:~8,2% PassDays
reg add "HKEY_CURRENT_USER/Control Panel/International" /v sShortDate /t REG_SZ /d %RegDateOld% /f>nul
set /a PassDays-=%DaysAgo%
call :DaysToDate %PassDays% DstYear DstMonth DstDay
set DstDate=%DstYear%%DstMonth%%DstDay%
set DstDate=%DstDate:~0,4%-%DstDate:~4,2%-%DstDate:~6,2%
for /d %%a in ("%SrcDir%/*.*") do (
if "%%~ta" leq "%DstDate%" (
if exist "%%a/"
echo rd /s /q "%%a"
)
)
)
reg add "HKEY_CURRENT_USER/Control Panel/International" /v sShortDate /t REG_SZ /d %RegDateOld% /f>nul
pause
goto :eof
: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
:DaysToDate %days% yy mm dd
setlocal ENABLEEXTENSIONS
set /a a=%1+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
(if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
endlocal&set %2=%yy%&set %3=%mm%&set %4=%dd%&goto :EOF
【方案三】BAT + VBS
复制代码 代码如下:
@echo off
::演示:删除指定路径下指定天数之前(以文件夹的最后修改日期为准)的文件夹。
::如果演示结果无误,把rd前面的echo去掉,即可实现真正删除。
::本例调用了临时VBS代码进行日期计算,并统一设置系统日期格式,处理完毕
::之后再把日期格式恢复成原来的状态。摆脱了对reg命令(XP系统自带)的依赖。
rem 指定待删除文件夹的存放路径
set SrcDir=C:/test
rem 指定天数
set DaysAgo=1
>"%temp%/BackupDate.vbs" echo Set WshShell = WScript.CreateObject("WScript.Shell")
"%temp%/BackupDate.vbs" echo WScript.Echo WshShell.RegRead ("HKEY_CURRENT_USER/Control Panel/International/sShortDate")
for /f %%a in ('cscript /nologo "%temp%/BackupDate.vbs"') do (
set "RegDateOld=%%a"
)
>"%temp%/UnifyDate.vbs" echo Set WshShell = WScript.CreateObject("WScript.Shell")
"%temp%/UnifyDate.vbs" echo WshShell.RegWrite "HKEY_CURRENT_USER/Control Panel/International/sShortDate", "yyyy-M-d", "REG_SZ"
cscript /nologo "%temp%/UnifyDate.vbs"
>"%temp%/DstDate.vbs" echo LastDate=date()-%DaysAgo%
"%temp%/DstDate.vbs" echo FmtDate=right(year(LastDate),4) ^& right("0" ^& month(LastDate),2) ^& right("0" ^& day(LastDate),2)
"%temp%/DstDate.vbs" echo wscript.echo FmtDate
for /f %%a in ('cscript /nologo "%temp%/DstDate.vbs"') do (
set "DstDate=%%a"
)
set DstDate=%DstDate:~0,4%-%DstDate:~4,2%-%DstDate:~6,2%
for /d %%a in ("%SrcDir%/*.*") do (
if "%%~ta" leq "%DstDate%" (
if exist "%%a/"
echo rd /s /q "%%a"
)
)
)
>"%temp%/RecoverDate.vbs" echo Set WshShell = WScript.CreateObject("WScript.Shell")
"%temp%/RecoverDate.vbs" echo WshShell.RegWrite "HKEY_CURRENT_USER/Control Panel/International/sShortDate", "%RegDateOld%", "REG_SZ"
cscript /nologo "%temp%/RecoverDate.vbs"
pause
【方案四】字符串截取
复制代码 代码如下:
@echo off
::演示:删除指定路径下指定天数之前(以文件夹名中包含的日期字符串为准)的文件夹。
::如果演示结果无误,把rd前面的echo去掉,即可实现真正删除。
::本例假设文件夹名中包含的日期字符串(比如:Archive-2009-12-25)
rem 指定待删除文件夹的存放路径
set SrcDir=C:/test
rem 指定天数
set DaysAgo=1
>"%temp%/DstDate.vbs" echo LastDate=date()-%DaysAgo%
"%temp%/DstDate.vbs" echo FmtDate=right(year(LastDate),4) ^& right("0" ^& month(LastDate),2) ^& right("0" ^& day(LastDate),2)
"%temp%/DstDate.vbs" echo wscript.echo FmtDate
for /f %%a in ('cscript /nologo "%temp%/DstDate.vbs"') do (
set "DstDate=%%a"
)
set DstDate=%DstDate:~0,4%-%DstDate:~4,2%-%DstDate:~6,2%
setlocal enabledelayedexpansion
for /d %%a in ("%SrcDir%/*.*") do (
if "%%~ta" leq "%DstDate%" (
set "FolderDate=%%~na"
set "FolderDate=!FolderDate:~8,10!"
if "!FolderDate!" leq "%DstDate%" (
if exist "%%a/" (
echo rd /s /q "%%a"
)
)
)
)
endlocal
pause
bat,删除,指定天数,文件夹
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]