【方案一】bat + reg + vbs
代碼如下:
@echo off
::演示:刪除指定路徑下指定天數(shù)之前(以文件夾的最后修改日期為準(zhǔn))的文件夾。
::如果演示結(jié)果無誤,把rd前面的echo去掉,即可實(shí)現(xiàn)真正刪除。
::本例調(diào)用了臨時(shí)vbs代碼進(jìn)行日期計(jì)算
::本例為兼容不同的日期格式,調(diào)用reg命令(xp系統(tǒng)自帶)統(tǒng)一設(shè)置日期格式,
::處理完畢之后再把日期格式恢復(fù)成原來的狀態(tài)。
rem 指定待刪除文件夾的存放路徑
set srcdir=c:/test
rem 指定天數(shù)
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 日期函數(shù)
代碼如下:
@echo off
::演示:刪除指定路徑下指定天數(shù)之前(以文件夾的最后修改日期為準(zhǔn))的文件夾。
::如果演示結(jié)果無誤,把rd前面的echo去掉,即可實(shí)現(xiàn)真正刪除。
::本例調(diào)用了 ritchie lawrence 的日期函數(shù)進(jìn)行日期計(jì)算
::日期轉(zhuǎn)換的核心算法請參考http://bbs.bathome.cn/thread-3056-1-1.html
::本例為兼容不同的日期格式,調(diào)用reg命令(xp系統(tǒng)自帶)統(tǒng)一設(shè)置日期格式,
::處理完畢之后再把日期格式恢復(fù)成原來的狀態(tài)。
rem 指定待刪除文件夾的存放路徑
set srcdir=c:/test
rem 指定天數(shù)
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
::演示:刪除指定路徑下指定天數(shù)之前(以文件夾的最后修改日期為準(zhǔn))的文件夾。
::如果演示結(jié)果無誤,把rd前面的echo去掉,即可實(shí)現(xiàn)真正刪除。
::本例調(diào)用了臨時(shí)vbs代碼進(jìn)行日期計(jì)算,并統(tǒng)一設(shè)置系統(tǒng)日期格式,處理完畢
::之后再把日期格式恢復(fù)成原來的狀態(tài)。擺脫了對reg命令(xp系統(tǒng)自帶)的依賴。
rem 指定待刪除文件夾的存放路徑
set srcdir=c:/test
rem 指定天數(shù)
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
::演示:刪除指定路徑下指定天數(shù)之前(以文件夾名中包含的日期字符串為準(zhǔn))的文件夾。
::如果演示結(jié)果無誤,把rd前面的echo去掉,即可實(shí)現(xiàn)真正刪除。
::本例假設(shè)文件夾名中包含的日期字符串(比如:archive-2009-12-25)
rem 指定待刪除文件夾的存放路徑
set srcdir=c:/test
rem 指定天數(shù)
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