2013-03-27 109 views
0

2日我使用它來獲取修改的文件的日期:比較在批處理文件

@echo off 
FOR %%? IN ("C:\some.exe") DO (
    ECHO Last-Modified Date : %%~t? 
) 

以上的回報是這樣的:最後修改日期:26/03/2013 14:43。

如何從上面取日期部分並將其與名稱中包含日期的另一個文件進行比較,例如:23-Sep-13.exe?

我需要能夠在批處理文件中執行一些代碼,如果名稱中包含日期的文件晚於文件修改版本即安裝更新。

回答

1
@ECHO OFF 
SETLOCAL 
:: No doubt for international use, this could be retrieved from the registry 
SET monthnames=Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 
:: get last modified date of target file - your format as specified 
:: (I used THIS BATCH FILE) 
FOR %%i IN ("%~f0") DO SET target=%%~ti 
:: parse the target date 
FOR /f "tokens=1-3delims=/ " %%i IN ("%target%") DO SET targetf=%%k%%j%%i 
:: 
:: parse name from 'other file' 
SET other=23-Sep-13.exe 
FOR /f "tokens=1-3delims=.- " %%i IN ("%other%") DO SET od=%%i&SET omn=%%j&SET oy=%%k 
:: Convert monthname to number 
:: @ECHO on 
SET om=101 
FOR %%i IN (%monthnames%) DO (
IF DEFINED omn IF /i %omn%==%%i (SET omn=) ELSE (SET /a om+=1) 
) 
:: Build date of 'other file' in same format (YYYYMMDD) 
SET otherf=20%oy%%om:~-2%%od% 
ECHO is %other% later than %target% ? 
ECHO IF %otherf% gtr %targetf% GOTO later 
ECHO. 
:: 
:: parse name from 'another other file' 
SET other=23-Jan-13.exe 
FOR /f "tokens=1-3delims=.- " %%i IN ("%other%") DO SET od=%%i&SET omn=%%j&SET oy=%%k 
:: Convert monthname to number 
SET om=101 
FOR %%i IN (%monthnames%) DO (
IF DEFINED omn IF /i %omn%==%%i (SET omn=) ELSE (SET /a om+=1) 
) 
:: Build date of 'other file' in same format (YYYYMMDD) 
SET otherf=20%oy%%om:~-2%%od% 
ECHO is %other% later than %target% ? 
ECHO IF %otherf% gtr %targetf% GOTO later 
ECHO. 

代碼需要(此批)的日期作爲目標(您'C:\some.exe' - 改變以適應然後

測試適用於兩種不同的filename-is-date+ext格式的文件名來測試

可以輕鬆調節。如果需要與20世紀的日期進行比較...... :)

+0

感謝彼得 - 這是一種享受。 你知道怎麼做,但使用exe文件版本而不是日期進行比較?即exe文件版本:4.5.6.0 =文件名:4050400.exe – 2013-04-02 21:23:51