簡體   English   中英

批量比較兩個文件的日期

[英]Comparing two files' dates in batch

set File1 = C:\filepath
set File2 =C:\filepath

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

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

IF "%DATE1%" GTR "%DATE2%" ECHO Files have same age && GOTO END

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

ECHO Newer file is "%NEWEST%"

嘗試這個:

@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

這取決於TIME設置,但是在大多數情況下應該可以使用。另一種方法是使用WMIC / WMI,但是我將需要一些時間來創建腳本

下面的答案中 get_file_c_time function 的問題是生成的時間可能超過 32 位 integer (例如 ftime=202212312359),並且 %%W 令牌被解釋為八進制 if the hour=%W小時在 00 和 09 之間。

為避免這些問題,請考慮以下版本(針對加拿大日期/時間制作,因此字段的順序和標記分隔符可能會因其他語言環境而改變):

rem get_file_c_time
rem   %1 path to file
rem   %2 variable to assign value
:get_file_c_time

set file_path=%~1

if not exist "%file_path%" echo File %1 does not exist && exit /b 1
if "%~2" equ "" echo Need a second parameter && exit /b 2

setlocal enableExtensions enableDelayedExpansion

for /f "skip=5 tokens=1,2,3,4,5,6 delims=/:.- " %%T in ('dir /tw "%file_path%"') do (
    set /a "month=1%%U-(11%%U-1%%U)/10"
    set /a "day=1%%V-(11%%V-1%%V)/10"
    set /a "hour=1%%W-(11%%W-1%%W)/10"
    set /a "minute=1%%X-(11%%X-1%%X)/10"
    set /a "ftime=%%T*535680+!month!*44640+!day!*1440+!hour!*60+!minute!"
    if "%%Y" EQU "PM" (
        set /a "ftime=!ftime!+720"
    )
    goto :endfor
)
:endfor

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM