Windows批处理 - 检查文件是否超过24小时

时间:2015-07-03 15:58:32

标签: date datetime batch-file compare

我使用此命令获取file date

for %%c in (%file_test%) do set date_test=%%~tc

我想将其与actual date

进行比较
set year=%date:~-4%
set month=%date:~3,2%
if "%month:~0,1%" == " " set month=0%month:~1,1%
set day=%date:~0,2%
if "%day:~0,1%" == " " set day=0%day:~1,1%

set hour=%time:~0,2%
if "%hour:~0,1%" == " " set hour=0%hour:~1,1%
set min=%time:~3,2%
if "%min:~0,1%" == " " set min=0%min:~1,1%

set mydate=%year%%month%%day%

有什么方法可以获得difference between both dates

包括月份没有相同的日期

如果file date has more than 24h

,我想检查

1 个答案:

答案 0 :(得分:0)

您可以使用:

@echo off
:: Wmic removes regional differences
:: XP Pro can have some filename errors due to the short filename bug
:: XP Home does not have WMIC.EXE

set NumMin=1440

call :CheckMins "%~f1"
:: echo %age%
goto :EOF

:CheckMins
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value')     
do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set     
"DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "stamp=%YYYY% %MM% %DD% %HH% %Min%"
call :DateToMinutes %stamp% NowMins
set "file=%~sf1"

:: can use CreationDate instead of lastmodified

WMIC DATAFILE WHERE name="%file:\=\\%" get lastmodified | find "." >file.tmp
for /f %%a in (file.tmp) do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "filestamp=%YYYY% %MM% %DD% %HH% %Min%"
del file.tmp 2>nul
if not defined yyyy goto :EOF

call :DateToMinutes %filestamp% FileMins

set /a MinsOld=%NowMins%-%FileMins%
if %MinsOld% gtr %NumMin% (set age=1) else (set age=0)
goto :EOF

:DateToMinutes
setlocal
set yy=%1&set mm=%2&set dd=%3&set hh=%4&set nn=%5
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
if 1%hh% LSS 20 set hh=0%hh%
if /i {%nn:~2,1%} EQU {p} if "%hh%" NEQ "12" set hh=1%hh%&set/a hh-=88
if /i {%nn:~2,1%} EQU {a} if "%hh%" EQU "12" set hh=00
if /i {%nn:~2,1%} GEQ {a} set nn=%nn:~0,2%
set /a hh=100%hh%%%100,nn=100%nn%%%100,j=j*1440+hh*60+nn
endlocal&set %6=%j%&goto :EOF

使用文件名作为变量调用它,例如: C:/scripts/checkage.bat C:/myfiles/filename.txt

这将检查"现在"之间的分钟数。和"修改日期"的文件。如果它大于1440分钟(一天),那么%AGE%变量将为1,否则它将为0.

可以修改为在第24行使用CreationDate而不是LastModified。