CMD batch: compare 2 files of the last modified times

August 8, 2012 at 20:40:41
Specs: Windows XP
Hi, I want to write a batch script that compare 2 files, to see if the difference of their last modified times are within 30 seconds (the purpose is I assume if the difference is more than 30s, those 2 files were not created together by an utility application). E.g. 13:35:53 & 13:36:11 are within 30s while 11:36:01 AM and 11:36:32 AM are NOT within 30s. The difficulties are may involve hour/minute/second calculation and time format (12hr / 24hr) of different PCs. How to achieve that? Thanks!

See More: CMD batch: compare 2 files of the last modified times

Report •

August 8, 2012 at 22:43:59
@echo off & setlocal enabledelayedexpansion
:this uses vbscript to overcome some of the problems you described.
:reference: script.chm for help with understanding foll.
>z.vbs echo wscript.stdout.write(abs(datediff("s",wscript.arguments(0),wscript.arguments(1))))
:load two file-times: date time am/pm from files spec. in commandline.
for %%a in (%1 %2) do (
:foll line corrected. apologies. now reads corr.
set z=!z! %%~ta
:f1-date f1-time f1-ampm f2-date f2-time f2-ampm
for /f "tokens=1-6" %%a in ("%z%") do (
:ok, if dates not same, i give up. default to "not-30-seconds"
if %%a neq %%d goto :noday
for /f %%z in ('cscript /nologo z.vbs %%b%%c %%e%%f') do set diff=%%z
echo %1 vs %2 times, in seconds: %diff%
goto :eof
set diff=69
echo dates differ, so assuming: %diff%

This "should" get diff in two files times, in units of seconds (in var %diff%) to which
you can compare. (f/e: if %diff% geq 30 goto :nolink)

Report •

August 8, 2012 at 23:15:14
Thanks nbrane but I'm a bit lost. I don't quite understand the script and when I tried to run the script, nth happens. Could you give more advice? Many thanks!

Report •

August 8, 2012 at 23:46:26
Sorry, (check PM messages). This worked ok on mine, but might not on yours. cscript.exe must be on your system (to see: dir /s cscript.exe). if not, this will blow out. The syntax of the bactchscript is: what.bat fname1 fname2
You might not have put the filenames, 1 and 2. You should get the size difference displayed if the filenames are on cmdline and if files exist.
f/e: two files, times to be compared are: TORP.TXT, CRANK.TXT
then should show:
TORP.TXT vs CRANK.TXT times in seconds: xx

Report •

Related Solutions

August 9, 2012 at 08:31:57
I like the idea, nbrane, but you might consider moving all of the date/time logic into VBScript:
With CreateObject("Scripting.FileSystemObject")
  WScript.Quit 1 And Abs(DateDiff("s", _
    .GetFile(WScript.Arguments(0)).DateLastModified, _
    .GetFile(WScript.Arguments(1)).DateLastModified)) <= CInt(WScript.Arguments(2))
End With

This should return 0 if the time difference between two files (args 1 and 2) is greater than the third argument, 1 if not.

How To Ask Questions The Smart Way

Report •

Ask Question