这里有一个可以在任何两个文件上使用的解决方案。
首先获取文件时间(参见如何在Windows命令行上获取文件的最后修改日期?)。
for %%a in (MyFile1.txt) do set File1Date=%%~ta
for %%a in (MyFile2.txt) do set File2Date=%%~ta
然而,用户必须手动将日期和时间分解为其组件,因为Cmd.exe会将它们作为字符串进行比较,因此2>10且10:00AM>2:00PM。
首先比较年份,然后是月份,日子,上午/下午,小时,最后是分钟和秒钟(实际上很耗时间,但我暂时没有更好的方法)。请参见最终代码结尾。
然而,如果文件的时间相同,但秒数不同,则此解决方案将无法使用。
如果需要这种精度,请使用“forfiles”命令获取文件时间(请参见
https://superuser.com/questions/91287/windows-7-file-properties-date-modified-how-do-you-show-seconds)。
for /F "tokens=*" %%a in ('forfiles /m MyFile1.txt /c "cmd /c echo @fdate @ftime"')
do set File1Date=%%a
for /F "tokens=*" %%a in ('forfiles /m MyFile2.txt /c "cmd /c echo @fdate @ftime"')
do set File2Date=%%a
请注意,“ForFiles”有一个限制,它不能处理带有空格的路径,因此如果您有带有空格的路径,则必须先更改到该目录,参见
forfiles - spaces in folder path。
比较代码
:compareFileTime
set "originalFileTime=%1"
set "secondFileTime=%2"
for /F "tokens=1,2,3 delims= " %%a in (%originalFileTime%) do (
set "originalDatePart=%%a"
set "originalTimePart=%%b"
set "originalAmPmPart=%%c"
)
for /F "tokens=1,2,3 delims= " %%a in (%secondFileTime%) do (
set "secondDatePart=%%a"
set "secondTimePart=%%b"
set "secondAmPmPart=%%c"
)
for /F "tokens=1,2,3 delims=/" %%a in ("%originalDatePart%") do (
set "originalMonthPart=%%a"
set "originalMonthDayPart=%%b"
set "originalYearPart=%%c"
rem We need to ensure that the year is in a 4 digit format and if not we add 2000 to it
rem Cmd considers "50" > "100" but 50 < 100, so don't surround it with qoutes
if %%c LSS 100 set "originalYearPart=20%%c
)
for /F "tokens=1,2,3 delims=/" %%a in ("%secondDatePart%") do (
set "secondMonthPart=%%a"
set "secondMonthDayPart=%%b"
set "secondYearPart=%%c"
rem We need to ensure that the year is in a 4 digit format and if not we add 2000 to it
rem Cmd considers "50" > "100" but 50 < 100, so don't surround it with quotes
if %%c LSS 100 set "secondYearPart=20%%c
)
if %originalYearPart% GTR %secondYearPart% goto newer
if %originalYearPart% LSS %secondYearPart% goto older
rem We reach here only if the year is identical
rem Cmd considers "2" > "10" but 2 < 10, so don't surround it with quotes or you will have to set the width explicitly
if %originalMonthPart% GTR %secondMonthPart% goto newer
if %originalMonthPart% LSS %secondMonthPart% goto older
if %originalMonthDayPart% GTR %secondMonthDayPart% goto newer
if %originalMonthDayPart% LSS %secondMonthDayPart% goto older
rem We reach here only if it is the same date
if %originalAmPmPart% GTR %secondAmPmPart% goto newer
if %originalAmPmPart% LSS %secondAmPmPart% goto older
rem we reach here only if i=t is the same date, and also the same AM/PM
for /F "tokens=1 delims=:" %%a in ("%originalTimePart%") do set "originalHourPart=%%a"
for /F "tokens=1 delims=:" %%a in ("%secondTimePart%") do set "secondHourPart=%%a"
rem Cmd considers "2" > "10" but 2 < 10, so don't surround it with qoutes or you will have to set the width explicitly
if %originalHourPart% GTR %secondHourPart% goto newer
if %originalHourPart% LSS %secondHourPart% goto older
rem The minutes and seconds can be compared directly
if %originalTimePart% GTR %secondTimePart% goto newer
if %originalTimePart% LSS %secondTimePart% goto older
if %originalTimePart% EQU %secondTimePart% goto same
goto older
exit /b
:newer
echo "newer"
exit /b
:older
echo "older"
exit /b
:same
echo "same"
exit /b