rem 设置备份保留天数
set DaysAgo=15
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%
rem 设置文件所在路径
set SrcDir=d:\backup\
rem 删过期文件
for /r "%SrcDir%" %%a in (*.bak) do (if "%%~ta" leq "%DstDate%" (del /f /q "%%a"))
reg add "HKEY_CURRENT_USER\Control Panel\International" /v sShortDate /t REG_SZ /d %RegDateOld% /f>nul
把以上代码保存到批处理文件,例如保存到C盘为db.bat,再到命令行运行下列命令
at 12:00 /INTERACTIVE /every:M,T,W,Th,F,S,Su "c:\db.bat"
计划任务就会在每天的中午12点删除你设定天数以前的bak文件。时间是24小时制,自己可修改