比较dos中的两个文件日期

时间:2014-06-25 18:28:43

标签: batch-file batch-processing

{ 设置File1 = C:\ filepath

设置File2 = C:\ filepath

FOR%i IN(%FILE1%)DO SET DATE1 = %% ~ti \

FOR%i IN(%FILE2%)DO SET DATE2 = %%〜ti

如果“%DATE1%”GTR“%DATE2%”ECHO文件具有相同的年龄&& GOTO END

FOR%i IN('DIR / B / O:D“%FILE1%”“%FILE2%”')DO SET NEWEST = %% i

ECHO较新的文件是“%NEWEST%”

1 个答案:

答案 0 :(得分:0)

试试这个:

@echo off
rem --compares the age of two files
rem --by Vasil "npocmaka" Arnaudov

call :isOlder     "C:\test.file1" "C:\test.file2"

goto :eof

:isOlder [%1 path to first file ; %2 path to second file]
    setlocal
        call :get_file_c_time  "%~1" time1
        call :get_file_c_time  "%~2" time2
    if "%time1%" LEQ "%time2%"  (
          echo YES
         ) else (
          echo NO
         )
goto :eof

:get_file_c_time [ %1 path to file; %2 variable to assign value  ]
        set file_path=%~1

        if not exist "%file_path%" echo file %1 does not exist&& exit /b 1

        if "%~2" equ "" echo need a secont parameter && exit /b 2

        setlocal enableDelayedExpansion

        for /f "skip=5 tokens=1,2,3,4,5,6 delims=/:.гчЈз " %%T in ('dir /tc "%file_path%"') do (
         if "%%Y" EQU "PM" (
          set /a "hour=%%W+12"
         ) else (
                set hour=%%W
         )
         set ftime=%%V%%U%%T!hour!%%Y
         goto :endfor
        )
:endfor

endlocal & set "%~2=%ftime%"
goto :eof

这取决于时间设置,但在大多数情况下应该工作.. 另一种方法是使用WMIC / WMI,但我需要一些时间来创建脚本