如何比较两个文件的创建日期? [英] How to compare two files' creation date?

查看:150
本文介绍了如何比较两个文件的创建日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用批处理为XP,我怎么可以比较两个文件的创建日期与函数isMoreRecentThan(pathToFile1,pathToFile2)返回true或false?

Using batch for XP, how can I compare the creation dates of two files with a function isMoreRecentThan(pathToFile1,pathToFile2) that returns true or false ?

该解决方案应该没有管理员权限运行(尤其不宜用WMIC)。

The solution should run without admin rights (and in particular should NOT use wmic).

推荐答案

这是在npocmaka的回答的最后一个编辑的版本,但略有修改。我已经介绍了这些Car981信息到code:

This is the last edit version in npocmaka's answer, but slightly modified. I have introduced these Car981's information into the code:


  • 函数isMoreRecentThan(文件1,文件2)如果file1比file2更近,则返回true,否则返回false

  • 在DIR日期格式为17/05/2013 15:01

该isMoreRecentThan子程序返回%ERRORLEVEL%等于1(真),如果第一个文件是比在其他任何情况下,第二个和0,其中如果有文件不存在更近。

The isMoreRecentThan subroutine return an %ERRORLEVEL% equal 1 (true) if first file is more recent than second one and 0 in any other case, including if any file does not exist.

@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
)

这篇关于如何比较两个文件的创建日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆