刪除指定路徑下指定天數之前(以文件的最後修改日期為準)的文件:BAT + REG + VBS
阿新 • • 發佈:2018-01-26
2.0 ane blog set logo fmt 演示 back style
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
@echo off ::演示:刪除指定路徑下指定天數之前(以文件的最後修改日期為準)的文件。 ::如果演示結果無誤,把del前面的echo去掉,即可實現真正刪除。 ::本例調用了臨時VBS代碼進行日期計算 ::本例為兼容不同的日期格式,調用reg命令(XP系統自帶)統一設置日期格式, ::處理完畢之後再把日期格式恢復成原來的狀態。 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
@echo off
::演示:刪除指定路徑下指定天數之前(以文件的最後修改日期為準)的文件。
::如果演示結果無誤,把del前面的echo去掉,即可實現真正刪除。
::本例調用了臨時VBS代碼進行日期計算
::本例為兼容不同的日期格式,調用reg命令(XP系統自帶)統一設置日期格式,
::處理完畢之後再把日期格式恢復成原來的狀態。
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 + VBS