[solved] Compare timestamps of two files (seconds too)
Moderator: DosItHelp
[solved] Compare timestamps of two files (seconds too)
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
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.
Re: ba
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.
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.
Re: ba
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
Re: ba
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
Dave Benham
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.
Re: Compare timestamps of two files (seconds too)
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
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
Re: Compare timestamps of two files (seconds too)
Based on your OS, you could always use PowerShell.
example.ps1This outputs the number of seconds, (to one decimal place), difference if found to be more than two seconds.
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")
Re: Compare timestamps of two files (seconds too)
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
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
Re: Compare timestamps of two files (seconds too)
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
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%
Re: Compare timestamps of two files (seconds too)
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?
Re: Compare timestamps of two files (seconds too)
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
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
Re: Compare timestamps of two files (seconds too)
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
Re: Compare timestamps of two files (seconds too)
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
Re: Compare timestamps of two files (seconds too)
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
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