Compare 2 dates in a batch file

后端 未结 1 1690
猫巷女王i
猫巷女王i 2020-12-22 05:47

I am using this to get the date modified of a file:

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

Th

相关标签:
1条回答
  • 2020-12-22 06:27
    @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.
    

    Code takes date of (this batch) as the target (your 'C:\some.exe'- change to suit.

    test then applied to two different filename-is-date+ext format filenames to test.

    Can be easily adjusted if comparisons to 20th-Century date is required... :)

    0 讨论(0)
提交回复
热议问题