在Windows批处理文件中,可以使用forfiles
命令来比较当前时间与文件的上次修改时间。以下是一个示例代码:
@echo off
set "file=C:\path\to\file.txt"
for %%F in ("%file%") do (
set "fileTime=%%~tF"
)
for /F "usebackq delims=" %%A in (`powershell -Command "(Get-Date).ToString('yyyyMMddHHmmss')"`) do set "currentTime=%%A"
echo File Time: %fileTime%
echo Current Time: %currentTime%
if "%fileTime%" GTR "%currentTime%" (
echo File has been modified after the current time.
) else if "%fileTime%" LSS "%currentTime%" (
echo File has been modified before the current time.
) else (
echo File has been modified at the current time.
)
在上面的代码中,首先定义了要比较的文件路径。然后使用for
命令获取文件的上次修改时间,并将其保存在变量fileTime
中。接着使用powershell
命令获取当前时间,并将其保存在变量currentTime
中。最后,使用if
语句比较fileTime
和currentTime
的值,输出相应的结果。
请注意,这个示例代码使用了PowerShell命令,因此需要确保系统上已经安装了PowerShell。另外,如果文件路径中包含空格,需要将文件路径用引号括起来。