0

我正在使用它来获取文件的修改日期:

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

以上返回类似:Last-Modified Date : 26/03/2013 14:43。

如何从上面获取日期部分并将其与另一个名称包含日期的文件进行比较,即:23-Sep-13.exe?

如果名称中包含日期的文件晚于文件修改版本(即安装更新),我需要能够在批处理文件中执行一些代码。

4

1 回答 1

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'- 更改以适应。

然后将 test 应用于两个不同filename-is-date+ext格式的文件名进行测试。

如果需要与 20 世纪日期进行比较,可以轻松调整... :)

于 2013-03-28T03:39:36.773 回答