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

Date Windows批处理-检查文件是否超过24小时,date,datetime,batch-file,compare,Date,Datetime,Batch File,Compare,我使用以下命令获取文件日期: for %%c in (%file_test%) do set date_test=%%~tc 我想将其与实际日期进行比较: 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:~

我使用以下命令获取
文件日期

for %%c in (%file_test%) do set date_test=%%~tc
我想将其与实际日期进行比较:

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%
有没有办法得到两个日期之间的
差异

包括月份的天数不相同

我想检查
文件日期是否超过24小时

您可以使用以下选项:

@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

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

您可以使用:

@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


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

组合减法和时间转换为分钟组合减法和时间转换为分钟我想说是我写的,但这是我得到帮助的代码。我记不起帮我做这件事的用户的名字了,但向那个家伙致敬。。。我想说是我写的,但这是我得到帮助的代码。我记不起帮我做这件事的用户的名字了,但向那个家伙致敬。。。他很有魅力