DosTips.com

A Forum all about DOS Batch
It is currently 22 Nov 2017 12:40

All times are UTC-06:00




Post new topic  Reply to topic  [ 17 posts ]  Go to page 1 2 Next
Author Message
PostPosted: 24 Nov 2014 09:20 
Offline

Joined: 26 Aug 2013 06:32
Posts: 14
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.

Top
   
 Post subject: Re: ba
PostPosted: 24 Nov 2014 09:49 
Offline
Expert

Joined: 23 Dec 2011 13:59
Posts: 3626
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.


Top
   
 Post subject: Re: ba
PostPosted: 24 Nov 2014 10:22 
Offline
Expert

Joined: 23 Dec 2011 13:59
Posts: 3626
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


Top
   
 Post subject: Re: ba
PostPosted: 24 Nov 2014 13:08 
Offline
Expert

Joined: 12 Feb 2011 21:02
Posts: 1953
Location: United States (east coast)
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.

Top
   
 Post subject: Re: ba
PostPosted: 24 Nov 2014 13:13 
Offline
Expert

Joined: 23 Dec 2011 13:59
Posts: 3626
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.


Top
   
 Post subject: Re: ba
PostPosted: 24 Nov 2014 17:51 
Offline
Expert

Joined: 22 Jan 2010 18:01
Posts: 2990
Location: Germany
@Squashman
Did you leave commas or equal signs unescaped or in an unquoted clause?

Regards
aGerman


Top
   
PostPosted: 25 Nov 2014 02:45 
Offline

Joined: 26 Aug 2013 06:32
Posts: 14
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


Top
   
PostPosted: 25 Nov 2014 05:52 
Offline

Joined: 21 Mar 2014 08:50
Posts: 452
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.


Top
   
PostPosted: 25 Nov 2014 06:37 
Offline
Expert

Joined: 12 Feb 2011 21:02
Posts: 1953
Location: United States (east coast)
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


Top
   
PostPosted: 26 Nov 2014 02:09 
Offline

Joined: 26 Aug 2013 06:32
Posts: 14
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%






Top
   
PostPosted: 26 Nov 2014 12:51 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
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?


Top
   
PostPosted: 26 Nov 2014 14:14 
Offline
Expert

Joined: 12 Feb 2011 21:02
Posts: 1953
Location: United States (east coast)
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


Top
   
PostPosted: 26 Nov 2014 14:31 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
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


Top
   
PostPosted: 26 Nov 2014 15:39 
Offline
Expert

Joined: 12 Feb 2011 21:02
Posts: 1953
Location: United States (east coast)
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


Top
   
PostPosted: 28 Nov 2014 09:07 
Offline

Joined: 26 Aug 2013 06:32
Posts: 14
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


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 17 posts ]  Go to page 1 2 Next

All times are UTC-06:00


Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited