3

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

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

4

3 回答 3

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子例程

于 2013-05-17T15:43:50.757 回答
2

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

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

如果第一个文件比第二个文件更新,isMoreRecentThan 子例程返回 %ERRORLEVEL% 等于 1 (true),在任何其他情况下返回 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
)
于 2013-05-18T05:10:13.827 回答
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

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

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

于 2013-05-18T03:20:54.317 回答