Batch bat deletes files before specified days and dates

Time:2019-8-15

[Solution 1] Delete files before the specified number of days under the specified path (whichever is the last modification date of the file): BAT + REG + VBS

Copy codeThe code is as follows:
@echo off
Demonstration: Delete the file before the specified number of days in the specified path, whichever is the last modification date of the file.
If the result of the demonstration is correct, the echo in front of del can be deleted.
:: This example calls the temporary VBS code to calculate the date.
This example is compatible with different date formats, calling reg command (XP system comes with it) to set the date format uniformly.
After processing, the date format will be restored to its original state.

REM specifies the storage path of the file to be deleted
set SrcDir=C:/Test/BatHome
REM specified 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(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 /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

[Solution 2] Delete files before the specified number of days under the specified path (whichever is the last modification date of the file): BAT + REG + Ritchie Lawrence Date Function

Copy codeThe code is as follows:
@echo off
Demonstration: Delete the file before the specified number of days in the specified path, whichever is the last modification date of the file.
If the result of the demonstration is correct, the echo in front of del can be deleted.
:: This example calls Ritchie Lawrence’s date function for date calculation.
:: For 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, calling reg command (XP system comes with it) to set the date format uniformly.
After processing, the date format will be restored to its original state.

REM specifies the storage path of the file to be deleted
set SrcDir=C:/Test/BatHome
REM specified 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 :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%
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

: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

[Solution 3] Delete files before the specified number of days under the specified path (whichever is the last modification date of the file): BAT + VBS

Copy codeThe code is as follows:
@echo off
Demonstration: Delete the file before the specified number of days in the specified path, whichever is the last modification date of the file.
If the result of the demonstration is correct, the echo in front of del can be deleted.
:: This example calls the temporary VBS code to calculate the date, and unifies the date format of the system, after processing.
:: Then restore the date format to its original state. Get rid of the dependence on Reg commands (XP system comes with it).

REM specifies the storage path of the file to be deleted
set SrcDir=C:/Test/BatHome
REM specified 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(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 /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

[Solution 4] Delete files before the specified number of days in the specified path (whichever date the file was created): BAT + REG + Ritchie Lawrence date function

Copy codeThe code is as follows:
@echo off
Demonstration: Delete files before the specified number of days in the specified path, whichever is the creation date of the file.
If the result of the demonstration is correct, the echo in front of del can be deleted.
:: This example calls Ritchie Lawrence’s date function for date calculation.
:: For 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, calling reg command (XP system comes with it) to set the date format uniformly.
After processing, the date format will be restored to its original state.

REM specifies the storage path of the file to be deleted
set SrcDir=C:/Test/BatHome
REM specified 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 :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%

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

: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

:CompareTime
for /f “skip=5 tokens=1-2 delims= ” %%h in (‘dir /a-d /tc %1’) do (
    if “%%h” leq “%DstDate%” (
        if exist %1 (
            echo del /a /f /q %1
        )
    )
    goto :eof
)

[Solution 5] Delete the file before the specified number of days under the specified path (whichever is the date of modification of the file):forfiles

Copy codeThe code is as follows:
@echo off
Demonstration: Delete the file before the specified number of days in the specified path, whichever is the last modification date of the file.
If the result of the demonstration is correct, the echo in front of del can be deleted.
:: This example needs the support of forfiles command in Win2003/Vista/Win7 system.

REM specifies the storage path of the file to be deleted
set SrcDir=C:/Test/BatHome
REM specified number of days
set DaysAgo=1

forfiles /p %SrcDir% /s /m *.* /d -%DaysAgo% /c “cmd /c echo del /f /q /a @path”
pause

[Solution 6] Delete a file before the specified number of days in the specified path (whichever date string is contained in the file name): String interception

Copy codeThe code is as follows:
@echo off
Demo: Delete the file before the specified number of days in the specified path (whichever date string is contained in the file name).
If the result of the demonstration is correct, the echo in front of del can be deleted.
:: This example assumes that the date string contained in the file name (for example, bak-2009-12-25.log)

REM specifies the storage path of the file to be deleted
set SrcDir=C:/Test/BatHome
REM specified number of days
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 /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