2013-03-27 106 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