batch-file - 检查文件是否早于 2 分钟

标签 batch-file executable

我想检查文件“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/

相关文章:

windows - 在批处理脚本中将文件路径连接到环境变量

batch-file - 批量编码 - 选择命令不返回正确答案

java - 用java写入.bat文件

linux - 如何在Centos桌面中通过命令行编辑 "File Management Preferences"

python - 循环可执行文件以从 Python 脚本获取结果

batch-file - 将文件存储在批处理文件中?

java - 需要指导来自动化多个 .bat 文件进程

来自可执行文件的控制流程图?

javascript - 从 webapp 运行外部程序

java - Maven 插件创建可执行 jar 与未解压的依赖项 (jar with jars)