下面的批处理文件将文件日期转换为儒略日数,即连续的天数,并用它来知道每个天数是多少天。删除文件的天数在参数中给出。
@echo off
setlocal EnableDelayedExpansion
rem Get Julian Day Number of today's date
rem The assumed format is MM/DD/YYYY, change C-A-B order in accordance with your locale
for /F "tokens=1-3 delims=/" %%a in ("%date%") do (
set todayDate=%%c%%a%%b
)
call :DateToJDN %todayDate% todayJDN=
for /F "tokens=1-3 delims=_." %%a in ('dir /B /A-D *.*') do (
call :DateToJDN %%b fileJDN=
set /A daysOld=todayJDN - fileJDN
if !daysOld! gtr %1 (
echo File "%%a_%%b.%%c" is !daysOld! days old
)
)
goto :EOF
:DateToJDN yyyymmdd jdn=
set yyyymmdd=%1
set /A yyyy=%yyyymmdd:~0,4%, mm=1%yyyymmdd:~4,2% %% 100, dd=1%yyyymmdd:~6% %% 100
set /A a=(mm-14)/12, %2=(1461*(yyyy+4800+a))/4+(367*(mm-2-12*a))/12-(3*((yyyy+4900+a)/100))/4+dd-32075
exit /B
测试这个程序并echo File "%%a_%%b.%%c" ...
用所需的命令更改命令del "%%a_%%b.%%c"
。
参考:http ://www.hermetic.ch/cal_stud/jdn.htm#comp