批处理bat删除指定天数日期之前的文件夹
【方案一】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
上一篇: CMD进入控制面板的命令小结
下一篇: MSG命令使用详解