2013-03-27 2 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 –

관련 문제