使用说明:第一个参数天数,第二个参数指定文件目录,省略时为当前路径
另外:以下实例只是将这些文件列出来,并没有删除,实际使用时请修改相应部分
实例一:系统日期格式为:2010-09-27 星期一
实例一完整代码
:: DateDel2.bat - 删除指定路径下指定日数以前修改的文件
:: Wonder - 2010-09-27 - CMD@Windows
:: 注意事项:参数%1指定日数,昨天为1,前天为2,依次类推
:: 参数%2指定文件路径,省略时将处理当前路径
:: 核心算法:Ritchie Lawrence, updated 2002-08-13. Version 1.1
:: 原作者:Will Sort
@echo off & setlocal ENABLEEXTENSIONS
@echo 执行开始:"%date% %time%" :
if "%1"=="" @echo "请输入保留天数"&goto :END
if "%2"=="" @echo "请输入目标目录"&goto :END
call :Date2Day %date:~0,10% sdays
set /a sdays-=%1
call :Day2Date %sdays% difdate
set opDir=%2
set filescount=0
echo execute %date% %time% 将删除 %opDir% 下 %difdate% 前的修改的以下文件:
for /r %opDir% %%f in (*.*) do if "%%~tf" LEQ "%difdate%" echo "%%f"&set /a filescount+=1
goto :END
:Date2Day
setlocal ENABLEEXTENSIONS
for /f "tokens=1-3 delims=/-, " %%a in ('echo/%1') do (
set yy=%%a & set mm=%%b & set dd=%%c
)
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 %2=%j%&goto :EOF
:Day2Date
setlocal ENABLEEXTENSIONS
set /a i=%1,a=i+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%-%mm%-%dd%&goto :EOF
:END
@echo 执行结束:"%date% %time%" 共计 %filescount% 个文件&echo -----------------------------------
实例二:系统日期格式为:Mon 09/27/2010
实例二完整代码
:: DateDel2.bat - Delete the Old files Modified several Days
:: Wonder Wang - 09/27/2010 - CMD@Windows 2003 English Version
:: Notes : Parameter %1 - number of the old days defined
:: : Parameter %2 - file path
:: Core Algorithm: Ritchie Lawrence, updated 2002-08-13. Version 1.1
::
@echo off & setlocal ENABLEEXTENSIONS
@echo Execute begin at "%date% %time%" :
set filescount=0
if "%1"=="" @echo "please input the first parameter :days "&goto :END
if "%2"=="" @echo "please input the second parameter :folder "&goto :END
call :Date2Day %date:~4,14% sdays
set /a sdays-=%1
call :Day2Date %sdays% difdate
set opDir=%2
echo execute at "%date% %time%" ,under folder %opDir% , the files before "%difdate%" will be deleted:
for /r %opDir% %%f in (*.*) do if "%%~tf" LEQ "%difdate%" echo "%%f"&set /a filescount+=1
goto :END
:Date2Day
setlocal ENABLEEXTENSIONS
for /f "tokens=1-4 delims=//, " %%a in ('echo/%1') do (
set mm=%%a & set dd=%%b & set yy=%%c
)
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 %2=%j%&goto :EOF
:Day2Date
setlocal ENABLEEXTENSIONS
set /a i=%1,a=i+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=%mm%/%dd%/%yy%&goto :EOF
:END
@echo Execute end at "%date% %time%" , effect %filescount% files&echo -----------------------------