我想检查文件“test.txt”是否超过两分钟。
到目前为止,我可以检查机器的当前时间戳和文件的最后修改日期。
但是如何从当前机器时间中减去 now 来检查文件是否早于两分钟?
set mytimestamp="%date:~0,2%.%date:~3,2%.%date:~6,4%
%time:~0,2%:%time:~3,2%"
echo %mytimestamp%
SET filename="test.txt"
FOR %%f IN (%filename%) DO SET filedatetime="%%~tf"
::echo %filedatetime%
IF %filedatetime% == %mytimestamp% goto same
goto notsame
:same
ECHO "files same"
goto :eof
:notsame
ECHO "files not same"
goto :eof
:eof
最佳答案
这应该有效:
set hh=%TIME:~0,2%
set mm=%TIME:~3,2%
set ss=%TIME:~6,2%
set /a sum_sec=%hh% * 3600 + %mm% * 60 + %ss% -120
set /a h=%sum_sec% / 3600
set /a m=(%sum_sec%/60) - 60 * %h%
set /a s=%sum_sec% - 60 * (%sum_sec%/60)
IF %h% LSS 10 set h=0%h%
IF %m% LSS 10 set m=0%m%
IF %s% LSS 10 set s=0%s%
set new_time=%h%:%m%:%s%
forfiles /P "C:\SomePath" /S /M test.txt /C "cmd /c if @ftime LSS %new_time% del /F /Q @path"
在此示例中,如果文件 test.txt 超过 2 分钟,我们将删除它。您可以通过将 120 替换为任何其他值(以秒为单位)来改变 set/a sum_sec=%hh% * 3600 + %mm% * 60 + %ss% -120
行中的时间。
关于batch-file - 检查文件是否早于 2 分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29694038/