时间:2022-06-12 09:35:39 | 栏目:DOS/BAT | 点击:次
【方案一】删除指定路径下指定天数之前(以文件的最后修改日期为准)的文件:BAT + REG + VBS
rem 指定待删除文件的存放路径
set SrcDir=C:/Test/BatHome
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 /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
【方案二】删除指定路径下指定天数之前(以文件的最后修改日期为准)的文件:BAT + REG + Ritchie Lawrence 日期函数
rem 指定待删除文件的存放路径
set SrcDir=C:/Test/BatHome
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%
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
【方案三】删除指定路径下指定天数之前(以文件的最后修改日期为准)的文件:BAT + VBS
rem 指定待删除文件的存放路径
set SrcDir=C:/Test/BatHome
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 /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
【方案四】删除指定路径下指定天数之前(以文件的创建日期为准)的文件:BAT + REG + Ritchie Lawrence 日期函数
rem 指定待删除文件的存放路径
set SrcDir=C:/Test/BatHome
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%
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
)
【方案五】删除指定路径下指定天数之前(以文件的修改日期为准)的文件:forfiles
rem 指定待删除文件的存放路径
set SrcDir=C:/Test/BatHome
rem 指定天数
set DaysAgo=1
forfiles /p %SrcDir% /s /m *.* /d -%DaysAgo% /c "cmd /c echo del /f /q /a @path"
pause
【方案六】删除指定路径下指定天数之前(以文件名中包含的日期字符串为准)的文件:字符串截取
rem 指定待删除文件的存放路径
set SrcDir=C:/Test/BatHome
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 /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