"Scenario One" deletes a file before the specified number of days in the specified path (whichever is the last modified date of the file): BAT + REG + VBS
Copy Code code as follows:
@echo off
:: Demo: Deletes a file that precedes a specified number of days in the specified path, whichever is the last modified date of the file.
:: If the demo results are correct, remove the echo in front of the Del, and you can realize the real deletion.
:: This example calls a temporary VBS code for a date calculation
:: This example is compatible with different date formats, call the REG command (XP system from the band) to set the date format uniformly,
:: After processing, then the date format back to the original state.
REM Specifies the location of the file to be deleted
Set Srcdir=c:/test/bathome
REM Specify the number of days
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 (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/R "%srcdir%"%%a in (*.*) do (
If "%%~ta" Leq "%dstdate%" (
If exist "%%a" (
echo del/f/q "%%a"
)
)
)
REG ADD "Hkey_current_user/control panel/international"/V sshortdate/t reg_sz/d%regdateold%/f>nul
Pause
"Scenario Two" deletes a file before the specified number of days in the specified path (whichever is the last modified date of the file): BAT + REG + Ritchie Lawrence Date function
Copy Code code as follows:
@echo off
:: Demo: Deletes a file that precedes a specified number of days in the specified path, whichever is the last modified date of the file.
:: If the demo results are correct, remove the echo in front of the Del, and you can realize the real deletion.
:: This example calls the date function of Ritchie Lawrence for date calculation
:: The core algorithm of date conversion please refer to http://bbs.bathome.cn/thread-3056-1-1.html
:: This example is compatible with different date formats, call the REG command (XP system from the band) to set the date format uniformly,
:: After processing, then the date format back to the original state.
REM Specifies the location of the file to be deleted
Set Srcdir=c:/test/bathome
REM Specify the number of days
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:D atetodays%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:D aystodate%passdays% dstyear dstmonth dstday
Set dstdate=%dstyear%-%dstmonth%-%dstday%
FOR/R "%srcdir%"%%a in (*.*) do (
If "%%~ta" Leq "%dstdate%" (
If exist "%%a" (
echo del/f/q "%%a"
)
)
)
REG ADD "Hkey_current_user/control panel/international"/V sshortdate/t reg_sz/d%regdateold%/f>nul
Pause
Goto:eof
:D atetodays%yy%%mm%%dd% days
Setlocal enableextensions
Set Yy=%1&set Mm=%2&set dd=%3
If 1%yy% LSS 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
:D aystodate%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 set mm=0%mm%) & (if%dd% LSS set dd=0%dd%)
Endlocal&set%2=%yy%&set%3=%mm%&set%4=%dd%&goto:eof
"Scenario three" deletes a file before the specified number of days in the specified path, whichever is the last modified date of the file: BAT + VBS
Copy Code code as follows:
@echo off
:: Demo: Deletes a file that precedes a specified number of days in the specified path, whichever is the last modified date of the file.
:: If the demo results are correct, remove the echo in front of the Del, and you can realize the real deletion.
:: This example calls the temporary VBS code for the date calculation, and sets the system date format uniformly, processing is complete
:: The date format is then restored to its original state. Get rid of dependencies on the reg command (XP system itself).
REM Specifies the location of the file to be deleted
Set Srcdir=c:/test/bathome
REM Specify the number of days
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 (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/R "%srcdir%"%%a in (*.*) do (
If "%%~ta" Leq "%dstdate%" (
If exist "%%a" (
echo del/f/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
Scenario four deletes a file before the specified number of days in the specified path (whichever is the date the file was created): BAT + REG + Ritchie Lawrence Date function
Copy Code code as follows:
@echo off
:: Demo: Deletes a file that precedes a specified number of days in the specified path, whichever is the date the file was created.
:: If the demo results are correct, remove the echo in front of the Del, and you can realize the real deletion.
:: This example calls the date function of Ritchie Lawrence for date calculation
:: The core algorithm of date conversion please refer to http://bbs.bathome.cn/thread-3056-1-1.html
:: This example is compatible with different date formats, call the REG command (XP system from the band) to set the date format uniformly,
:: After processing, then the date format back to the original state.
REM Specifies the location of the file to be deleted
Set Srcdir=c:/test/bathome
REM Specify the number of days
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:D atetodays%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:D aystodate%passdays% dstyear dstmonth dstday
Set dstdate=%dstyear%-%dstmonth%-%dstday%
for/f "delims=/"%%a in (' dir/s/b/a-d "%srcdir%" ') Do (
Call:comparetime "%%a"
)
REG ADD "Hkey_current_user/control panel/international"/V sshortdate/t reg_sz/d%regdateold%/f>nul
Pause
Goto:eof
:D atetodays%yy%%mm%%dd% days
Setlocal enableextensions
Set Yy=%1&set Mm=%2&set dd=%3
If 1%yy% LSS 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
:D aystodate%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 set mm=0%mm%) & (if%dd% LSS set dd=0%dd%)
Endlocal&set%2=%yy%&set%3=%mm%&set%4=%dd%&goto:eof
: Comparetime
for/f "skip=5 tokens=1-2 delims="%%h in (' dir/a-d/tc%1 ') does (
If "%%h" Leq "%dstdate%" (
If exist%1 (
Echo del/a/f/q%1
)
)
Goto:eof
)
"Scenario Five" deletes a file before the specified number of days in the specified path, whichever is the modified date of the file: Forfiles
Copy Code code as follows:
@echo off
:: Demo: Deletes a file that precedes a specified number of days in the specified path, whichever is the last modified date of the file.
:: If the demo results are correct, remove the echo in front of the Del, and you can realize the real deletion.
:: This example requires the support of the Forfiles command from the win2003/vista/win7 system
REM Specifies the location of the file to be deleted
Set Srcdir=c:/test/bathome
REM Specify the number of days
Set Daysago=1
forfiles/p%srcdir%/s/m *.*/d-%daysago%/C "cmd/c Echo del/f/q/a @path"
Pause
Scenario six deletes a file that precedes a specified number of days in the specified path, whichever is the date string contained in the file name: string interception
Copy Code code as follows:
@echo off
:: Demo: Deletes a file that precedes a specified number of days in the specified path, whichever is the date string contained in the file name.
:: If the demo results are correct, remove the echo in front of the Del, and you can realize the real deletion.
:: This example assumes a date string contained in the filename (for example, Bak-2009-12-25.log)
REM Specifies the location of the file to be deleted
Set Srcdir=c:/test/bathome
REM Specify the number of days
Set Daysago=1
> "%temp%/dstdate.vbs" Echo lastdate=date ()-%daysago%
>> "%temp%/dstdate.vbs" Echo fmtdate=right (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/R "%srcdir%"%%a in (*.*) do (
Set "Filedate=%%~na"
Set "filedate=! filedate:~4,10! "
If "! filedate! "Leq"%dstdate% "(
If exist "%%a" (
echo del/f/q "%%a"
)
)
)
Endlocal
Pause