[solved] Compare timestamps of two files (seconds too)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
kadkam
Posts: 14
Joined: 26 Aug 2013 06:32

[solved] Compare timestamps of two files (seconds too)

#1 Post by kadkam » 24 Nov 2014 09:20

hi

file A: test.txt
file B: pippo.txt

I need a simple batch that compare timestamps (modified date) of two files in different folders.
Is important to check seconds too; It will be detect difference if value is over 2 seconds.

My o.s. is Windows 7 and time in european format (24h)

thanks
Last edited by kadkam on 01 Dec 2014 10:25, edited 7 times in total.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: ba

#2 Post by Squashman » 24 Nov 2014 09:49

Which Date and Time? Created Date, Modified Date or Last Accessed date?

I don't think this will be possible with pure batch as the DIR command and the command modifiers only output up to minutes. Might have to go to Vbscript or Jscript to get the file time down to seconds.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: ba

#3 Post by Squashman » 24 Nov 2014 10:22

This will get you the FILE times down to seconds in Windows Vista and Above. If you are running Windows XP you will need to install the FORFILES command.

Code: Select all

@echo off
FOR /F "delims=" %%G in ('forfiles /m FileA.txt /c "cmd /c echo @ftime"') do set fileAtime=%%G
FOR /F "delims=" %%G in ('forfiles /m FileB.txt /c "cmd /c echo @ftime"') do set fileBtime=%%G

echo %fileAtime%
echo %fileBtime%
pause

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: ba

#4 Post by dbenham » 24 Nov 2014 13:08

Here is a locale indpendent solution that works as long as your machine has WMIC.
Edited to support single quote always, and comma if short names are enabled

Code: Select all

@echo off

call :FileModTime test.txt A
call :FileModTime pippo.txt B
set "diff=0"
if defined A if defined B set /a diff=B-A
if %diff% gtr 2 echo xxxx
exit /b


:FileModTime  File  [TimeVar]
::
::  Computes the Unix time (epoch time) for the last modified timestamp for File.
::  The result is an empty string if the file does not exist. Stores the result
::  in TimeVar, or prints the result if TimeVar is not specified.
::
::  Unix time = number of seconds since midnight, January 1, 1970 GMT
::
setlocal disableDelayedExpansion
:: Get full path of file (short form if possible)
for %%F in ("%~1") do set "file=%%~sF"
:: Get last modified time in YYYYMMDDHHMMSS format
set "time="
for /f "skip=1 delims=,. tokens=2" %%A in (
  'wmic datafile where name^="%file:\=\\%" get lastModified /format:csv 2^>nul'
) do set "ts=%%A"
set "ts=%ts%"
:: Convert time to Unix time (aka epoch time)
if defined ts (
  set /a "yy=10000%ts:~0,4% %% 10000, mm=100%ts:~4,2% %% 100, dd=100%ts:~6,2% %% 100"
  set /a "dd=dd-2472663+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4"
  set /a "ss=(((1%ts:~8,2%*60)+1%ts:~10,2%)*60)+1%ts:~12,2%-366100-%ts:~21,1%((1%ts:~22,3%*60)-60000)"
  set /a "ts=ss+dd*86400"
)
:: Return the result
endlocal & if "%~2" neq "" (set "%~2=%ts%") else echo(%ts%


Dave Benham
Last edited by dbenham on 26 Nov 2014 14:11, edited 2 times in total.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: ba

#5 Post by Squashman » 24 Nov 2014 13:13

Ugh!!! I kept playing around with WMIC and couldn't get it too work. If I typed the line at the command prompt it would work fine. Once I put it into a FOR command I would get an error Invalid Alias Verb.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: ba

#6 Post by aGerman » 24 Nov 2014 17:51

@Squashman
Did you leave commas or equal signs unescaped or in an unquoted clause?

Regards
aGerman

kadkam
Posts: 14
Joined: 26 Aug 2013 06:32

Re: Compare timestamps of two files (seconds too)

#7 Post by kadkam » 25 Nov 2014 02:45

thanks for attention.

I update first post;

I need a simple batch that compare timestamps (modified date) of two files in different folders.
Is important to check seconds too; It will be detect difference if value is over 2 seconds.

My o.s. is Windows 7 and time in european format (24h)


If can help, times ago I asked a similar question and I see that seconds are checked here:
viewtopic.php?f=3&t=4880

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Compare timestamps of two files (seconds too)

#8 Post by Compo » 25 Nov 2014 05:52

Based on your OS, you could always use PowerShell.
example.ps1

Code: Select all

$file1 = "C:\Users\kadkam\Desktop\NewLog.log"
$file2 = "D:\Data\WorkFiles\Test\AnyLog.log"

$Diff = ((ls $file1).LastWriteTime-(ls $file2).LastWriteTime).duration().TotalSeconds

If ($Diff -gt 2) {
   "{0:N1}" -f $Diff
}

Write-Host "Press any key to close ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
This outputs the number of seconds, (to one decimal place), difference if found to be more than two seconds.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Compare timestamps of two files (seconds too)

#9 Post by dbenham » 25 Nov 2014 06:37

Did you even look at my answer :?
Everything you need is right there.

The FileModTime routine uses WMIC (standard on your machine) to get the last modified timestamp of whatever file you pass it. You can include path information with the file name. It converts the timestamp into number of seconds since midnight, Jan 1, 1970, GMT (UTC). By locale independent, I mean that it works no matter how your machine is configured to display date and time.


Dave Benham

kadkam
Posts: 14
Joined: 26 Aug 2013 06:32

Re: Compare timestamps of two files (seconds too)

#10 Post by kadkam » 26 Nov 2014 02:09

Thanks Squash for your "concept", I will study it.
Interesting the Compo solution with powershell ! I will study it too.

Dear Dave

Thanks for your work, I'm using your solution.

I completed your code

Code: Select all

@echo off

call :FileModTime test.txt A
call :FileModTime pippo.txt B
set "diff=0"
if defined A if defined B set /a diff=B-A
if %A% gtr %B% if %diff% lss -2 echo file A newer
if %B% gtr %A% if %diff% gtr 2 echo file B newer
if %diff% leq 2 if %diff% geq -2 echo files timestamp is identical or in tolerance (2 seconds)

pause
exit /b



:FileModTime  File  [TimeVar]
::
::  Computes the Unix time (epoch time) for the last modified timestamp for File.
::  The result is an empty string if the file does not exist. Stores the result
::  in TimeVar, or prints the result if TimeVar is not specified.
::
::  Unix time = number of seconds since midnight, January 1, 1970 GMT
::
setlocal disableDelayedExpansion
:: Get full path of file
for %%F in ("%~1") do set "file=%%~fF"
:: Get last modified time in YYYYMMDDHHMMSS format
set "time="
for /f "skip=1 delims=,. tokens=2" %%A in (
  'wmic datafile where "name='%file:\=\\%'" get lastModified /format:csv 2^>nul'
) do set "ts=%%A"
set "ts=%ts%"
:: Convert time to Unix time (aka epoch time)
if defined ts (
  set /a "yy=10000%ts:~0,4% %% 10000, mm=100%ts:~4,2% %% 100, dd=100%ts:~6,2% %% 100"
  set /a "dd=dd-2472663+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4"
  set /a "ss=(((1%ts:~8,2%*60)+1%ts:~10,2%)*60)+1%ts:~12,2%-366100-%ts:~21,1%((1%ts:~22,3%*60)-60000)"
  set /a "ts=ss+dd*86400"
)
:: Return the result
endlocal & if "%~2" neq "" (set "%~2=%ts%") else echo(%ts%





foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Compare timestamps of two files (seconds too)

#11 Post by foxidrive » 26 Nov 2014 12:51

dbenham wrote:my answer
it works no matter how your machine is configured to display date and time.
Dave Benham


It does have a flaw with some filename characters, I think.

Does ' and , work?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Compare timestamps of two files (seconds too)

#12 Post by dbenham » 26 Nov 2014 14:14

Good point. Fixing single quote is easy. But comma can be a problem, as you know, (and I had forgotten).

Use of full path short names solves the problem, but only if short names are enabled. Without short names, there is no universal solution. There is no known WMIC long name solution that works with both comma and closing parenthesis.

I've updated my answer to be as robust as possible. (Well, I suppose it could be modified to use one syntax if comma is found, and another if closing paren, but meh)

Thanks for pointing this out foxi


Dave Benham

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Compare timestamps of two files (seconds too)

#13 Post by foxidrive » 26 Nov 2014 14:31

dbenham wrote:Good point. Fixing single quote is easy. But comma can be a problem, as you know, (and I had forgotten).

Thanks for pointing this out foxi


It's been a long while since I saw that thread Dave, and it has some interesting alternatives here:
viewtopic.php?p=32157#p32157

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Compare timestamps of two files (seconds too)

#14 Post by dbenham » 26 Nov 2014 15:39

foxidrive wrote:It's been a long while since I saw that thread Dave, and it has some interesting alternatives here:
viewtopic.php?p=32157#p32157

I hate the flashing of the mshta. Also, the code there fails with single quote in the name. There must be a way to make it work, but I haven't figured it out.

Couldn't hybrid JScript/batch work just as well?
If not, then surely Liviu's batch/wsf/vbs hybrid would work well.


Dave Benham

kadkam
Posts: 14
Joined: 26 Aug 2013 06:32

Re: Compare timestamps of two files (seconds too)

#15 Post by kadkam » 28 Nov 2014 09:07

Hi Dave

After some tests I founded this issues:

pippo.txt OK
c:\TEST\pippo.txt OK
c:\TEST 1\pippo.txt FAILED
c:\TEST\pippo,1.txt FAILED

Post Reply