如何比较两个文件的创建日期?

时间:2013-05-17 15:34:25

标签: batch-file

使用batch for XP,如何将两个文件的创建日期与返回true或false的函数isMoreRecentThan(pathToFile1,pathToFile2)进行比较?

解决方案应该在没有管理员权限的情况下运行(特别是不应该使用wmic)。

3 个答案:

答案 0 :(得分:2)

for /f "tokens=1,2,3 delims=/" %%T in ('forfiles /m file1 /C "cmd /c echo @fdate"') do set date1=%%V%%U%%T
for /f "tokens=1,2,3 delims=/" %%T in ('forfiles /m file2 /C "cmd /c echo @fdate"') do set date2=%%V%%U%%T
if %date2% GTR %date1% *some command here*

您还可以使用@ftime进行更精确的比较。和。 forfiles不是Windows XP和2003的本机命令,如果您是必须下载并安装Windows 2003资源工具包工具不使用较新的操作系统

编辑(适用于XP和2003):

setlocal enabledelayedexpansion
for /f "tokens=1,2,3,4,5,6 delims=/: " %%T in ('for %%a in ^(FILE1^) do echo %%~ta') do (
 if "%%Y" EQU "PM" (
  set /a "hour=%%W+12"
 ) else (
    set hour=%%W
 )
 set time1=%%V%%U%%T!hour!%%Y
 )

 for /f "tokens=1,2,3,4,5,6 delims=/: " %%T in ('for %%a in ^(FILE2^) do echo %%~ta') do (
 if "%%Y" EQU "PM" (
  set /a "hour=%%W+12"
 ) else (
    set hour=%%W
 )
 set time2=%%V%%U%%T!hour!%%Y
 )
 if !time1! GTR !time2! *do something*

 endlocal 

您需要使用相关值更改FILE1和FILE2

最后编辑

@echo off


call :isOlder     "C:\test.file1" "C:\test.file2"

goto :eof

:isOlder [%1 path to first file ; %2 path to second file]
    setlocal
    call :get_file_c_time  "%~1" time1
    call :get_file_c_time  "%~2" time2
    if "%time1%" LEQ "%time2%"  ( 
      echo YES 
     ) else (
      echo NO
     )
goto :eof

:get_file_c_time [ %1 path to file; %2 variable to assign value  ]
    set file_path=%~1

    if not exist "%file_path%" echo file %1 does not exist&& exit /b 1

    if "%~2" equ "" echo need a secont parameter && exit /b 2

    setlocal enableDelayedExpansion

    for /f "skip=5 tokens=1,2,3,4,5,6 delims=/:.гчЈз " %%T in ('dir /tc "%file_path%"') do (
     if "%%Y" EQU "PM" (
      set /a "hour=%%W+12"
     ) else (
        set hour=%%W
     )
     set ftime=%%V%%U%%T!hour!%%X
     goto :endfor
    )
:endfor

endlocal & set "%~2=%ftime%"
goto :eof

现在您可以使用:isOlder 子例程

答案 1 :(得分:2)

@ECHO OFF
SETLOCAL
:: files to be compared
SET "filea=c:\mys\oldobj.txt"
SET "fileb=c:\mys\15850492.htm"
:: Create a tempdir
:Tloop
SET tempdir="%temp%\%random%"
IF EXIST %tempdir%* GOTO tloop
MD %tempdir%

MOVE "%filea%" %tempdir%\a >nul
MOVE "%fileb%" %tempdir%\b >NUL
SET file
ECHO.
FOR /f %%i IN ('dir /b /a-d /o-d /tc %tempdir%') DO SET first=%%i
ECHO first file (create date) was %first%
FOR /f %%i IN ('dir /b /a-d /o-d /tw %tempdir%') DO SET first=%%i
ECHO first file (write date) was %first%
MOVE %tempdir%\a "%filea%" >nul
MOVE %tempdir%\b "%fileb%" >nul
RD %tempdir%

ECHO.
ECHO create times=v=v=v=v=v=v=v=v=v=v=v
ECHO.
DIR %filea% %fileb% /od /tc
ECHO.
ECHO write times=v=v=v=v=v=v=v=v=v=v=v
ECHO.
DIR %filea% %fileb% /od /tw

测试结果:(不相关的行删除)

filea=c:\mys\oldobj.txt
fileb=c:\mys\15850492.htm

first file (create date) was a
first file (write date) was b

create times=v=v=v=v=v=v=v=v=v=v=v

 Directory of c:\mys

07/07/2012  21:40           788,960 oldobj.txt
07/04/2013  19:06            16,295 15850492.htm

write times=v=v=v=v=v=v=v=v=v=v=v

 Directory of c:\mys

07/04/2013  19:06            16,295 15850492.htm
10/05/2013  14:04           788,960 oldobj.txt

所以 - 是的 - 使用这种方法,文件是否在同一目录中非常相关,因为它依赖于在相关日期对文件进行排序。

正如所示,创建日期和上次更新日期可能会有很大差异 - 这取决于文件的用途。甚至可能有一个晚于最后更新日期的创建日期 - 实际上它是常见的,因为如果文件是COPIED,新文件获取它创建的日期,但最后更新日期就是 - 原始文件的最后更新日期。

答案 2 :(得分:2)

这是npocmaka答案中的最后一个编辑版本,但略有修改。我已将这些Car981的信息引入代码中:

  • 一个函数isMoreRecentThan(file1,file2),如果file1比file2更新,则返回true,否则返回false
  • 目录中的日期格式为17/05/2013 15:01

如果第一个文件比第二个文件更新,则isMoreRecentThan子例程返回%ERRORLEVEL%等于1(真),在任何其他情况下返回0,包括任何文件不存在。

@echo off

call :isMoreRecentThan "C:\path1\file1.ext" "C:\path2\file2.ext"
if %errorlevel% equ 1 echo File1 is more recent than File2
goto :eof


:isMoreRecentThan [%1=path to first file ; %2=path to second file]
setlocal
call :get_file_c_time "%~1" time1
call :get_file_c_time "%~2" time2
set exitCode=0
if %time1% neq 0 if %time2% neq 0 if "%time1%" gtr "%time2%" set exitCode=1
exit /B %exitCode%


:get_file_c_time [ %1 path to file; %2 variable to assign value  ]
set %2=0
if not exist %1 exit /B
for /F "skip=5 tokens=1-5 delims=/: " %%a in ('dir /TC %1') do (
   set %2=%%c%%b%%a%%d%%e
   exit /B
)