File Comparison by Date Time

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
imagination
Posts: 13
Joined: 24 May 2014 01:32

File Comparison by Date Time

#1 Post by imagination » 26 May 2014 01:34

Good morning All,

I am having some trouble of comparing an list of files in 1 dir to an list of files in an other dir by date time

What i have so far is this

Code: Select all

@echo off
SETLOCAL EnableDelayedExpansion

REM GETTING FILES IN RESCLIENT
FOR /R e:\Pandoraflyff\Resclient %%A IN (*.*) do (
   SET Original=%%~TA
   SET FileName=%%A
)

REM GETTING FILES IN PATHCER
set newOrMod=
FOR /R e:\Pandoraflyff\Patcher %%B IN (*.*) do (
   SET Compare=%%~TB
   SET FileNameCompare=%%B
   if !Original! LSS !Compare! ( GOTO OLDER ) ELSE ( GOTO NEWER )
)

:OLDER
echo file !FileName! !Original! is older then file !FileNameCompare! !Compare!

:NEWER
echo !Original! !Compare!


The output is this
e:\Pandoraflyff\Resclient\World\WdVolcanceYellow\Pandora_00.res 11-05-2014 is older then e:\Pandoraflyff\Patcher\Flyff.a.gz 26-05-2014

So it looks to me that the for loop isnt correct. cause it compares 2 different files.
What i need is that its checking all the files in folder1 and its subfolders and compare them to all the files in folder2 and its subfolders
Also it must compare according the file name
so

e:\folder1\file1.res against e:\folder2\file1.res.gz
e:\folder1\file2.res against e:\folder2\file2.res.gz

And not like the above comparison.
Any help would be appriciated.
Btw files are not going to be changed within 1 minute so there fore i can use an batch file.

With kind regards,

Thomas de Vries

julesverne
Posts: 81
Joined: 19 Nov 2013 00:41

Re: File Comparison by Date Time

#2 Post by julesverne » 26 May 2014 02:04

dostips has this as one of their tricks, that may be useful to you. Meanwhile I'll keep checking to see if I can figure out what the issue is.

http://www.dostips.com/DtTipsDateTime.php#Function.CmpFTime

julesverne
Posts: 81
Joined: 19 Nov 2013 00:41

Re: File Comparison by Date Time

#3 Post by julesverne » 26 May 2014 02:25

Taking some time looking at your code you will never compare two files. The reason is because you need to embed the 2nd loop into the 1st loop, so that file %a matches a filename in the 2nd loop. and then it can perform the date comparison.

The output you're seeing is the last file each of your for loops found.

I edited this line to match your file data that I missed in your original post.
if "%%A" EQU "%%B.gz" if !Original! LSS !Compare! ( Call OLDER ) ELSE ( Call NEWER )


revised edit

if "%%A.gz" EQU "%%B" if !Original! LSS !Compare! ( Call OLDER ) ELSE ( Call NEWER )

Code: Select all

@echo off
SETLOCAL EnableDelayedExpansion

REM GETTING FILES IN RESCLIENT
FOR /R e:\Pandoraflyff\Resclient %%A IN (*.*) do (
   SET Original=%%~TA
   SET FileName=%%A
   FOR /R e:\Pandoraflyff\Patcher %%B IN (*.*) do (
         SET Compare=%%~TB
         SET FileNameCompare=%%B
         if "%%A.gz" EQU "%%B" if !Original! LSS !Compare! ( Call OLDER ) ELSE ( Call NEWER )
   )
)

:OLDER
echo file !FileName! !Original! is older then file !FileNameCompare! !Compare!

goto :eof
:NEWER
echo !Original! !Compare!
goto :eof



try that out
Last edited by julesverne on 26 May 2014 03:45, edited 4 times in total.

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

Re: File Comparison by Date Time

#4 Post by Compo » 26 May 2014 03:07

Depending upon what exactly you are wanting to do with the output and upon knowing the filenames and path you may even get away with something as simple as this!

Code: Select all

@Echo off & SetLocal
(Set _SRC=E:\Pandoraflyff\Patcher)
(Set _DST=E:\Pandoraflyff\Resclient)
For /f "Tokens=1* Delims=->" %%A In ('XCopy "%_SRC%" "%_DST%" /dflsuy') Do (
   If %%B' NEq ' Echo(%%B is older than %%A)
Pause >Nul

julesverne
Posts: 81
Joined: 19 Nov 2013 00:41

Re: File Comparison by Date Time

#5 Post by julesverne » 26 May 2014 03:20

Nice. I was looking at dostips somewhere and noticed the xcopy method, very interesting. Doesn't Robocopy have a way to do that too? Where it won't copy it'll just give you a list and you can set your switches to include certain file attributes that you can in this case compare the two files against?

Compo wrote:Depending upon what exactly you are wanting to do with the output and upon knowing the filenames and path you may even get away with something as simple as this!

Code: Select all

@Echo off & SetLocal
(Set _SRC=E:\Pandoraflyff\Patcher)
(Set _DST=E:\Pandoraflyff\Resclient)
For /f "Tokens=1* Delims=->" %%A In ('XCopy "%_SRC%" "%_DST%" /dflsuy') Do (
   If %%B' NEq ' Echo(%%B is older than %%A)
Pause >Nul

imagination
Posts: 13
Joined: 24 May 2014 01:32

Re: File Comparison by Date Time

#6 Post by imagination » 26 May 2014 03:20

Hmm i have tried it out.

However i am now getting the last file in both for loops and not all files

The output is now only
file e:\Pandoraflyff\Resclient\Word\WdVolcaneYellow\Pandora_00.res is older then e:\Pandoraflyff\Patch\Word\WdVolcaneYellow\Pandora_00.res.gz

While in fact it needs to compare all the files.

So i have tried this out

Code: Select all

REM GETTING FILES IN RESCLIENT
FOR /R e:\Pandoraflyff\Resclient %%A IN (*.*) do (
   SET Original=%%~TA
   SET FileName=%%A
   FOR /R e:\Pandoraflyff\Patcher %%B IN (*.*) do (
         SET Compare=%%~TB
         SET FileNameCompare=%%B
         if %%A EQU %%B if !Original! LSS !Compare! ( Call OLDER ) ELSE ( Call NEWER )
   
         :OLDER
         echo file !FileName! !Original! is older then file !FileNameCompare! !Compare!   
   )
)


But then the outcome is like this
file e:\Pandoraflyff\Resclient\Flyff.a is Older then e:\Pandoraflyff\Patch\Flyff.a.gz
file e:\Pandoraflyff\Resclient\Flyff.a is Older then e:\Pandoraflyff\Patch\Neuz.exe.gz

So its checking 1 file in folder 1 to all files in folder2.

imagination
Posts: 13
Joined: 24 May 2014 01:32

Re: File Comparison by Date Time

#7 Post by imagination » 26 May 2014 03:25

Compo wrote:Depending upon what exactly you are wanting to do with the output and upon knowing the filenames and path you may even get away with something as simple as this!

Code: Select all

@Echo off & SetLocal
(Set _SRC=E:\Pandoraflyff\Patcher)
(Set _DST=E:\Pandoraflyff\Resclient)
For /f "Tokens=1* Delims=->" %%A In ('XCopy "%_SRC%" "%_DST%" /dflsuy') Do (
   If %%B' NEq ' Echo(%%B is older than %%A)
Pause >Nul



Hmm i have looked at this but it aint putting this out.
Maybe because the SRC = .res and the DST = .gz

julesverne
Posts: 81
Joined: 19 Nov 2013 00:41

Re: File Comparison by Date Time

#8 Post by julesverne » 26 May 2014 03:33

I missed some data you stated in your original post so I edited my first reply above. Make that change and try it again.

imagination wrote:Hmm i have tried it out.

However i am now getting the last file in both for loops and not all files

The output is now only
file e:\Pandoraflyff\Resclient\Word\WdVolcaneYellow\Pandora_00.res is older then e:\Pandoraflyff\Patch\Word\WdVolcaneYellow\Pandora_00.res.gz

While in fact it needs to compare all the files.

So i have tried this out

Code: Select all

REM GETTING FILES IN RESCLIENT
FOR /R e:\Pandoraflyff\Resclient %%A IN (*.*) do (
   SET Original=%%~TA
   SET FileName=%%A
   FOR /R e:\Pandoraflyff\Patcher %%B IN (*.*) do (
         SET Compare=%%~TB
         SET FileNameCompare=%%B
         if %%A EQU %%B if !Original! LSS !Compare! ( Call OLDER ) ELSE ( Call NEWER )
   
         :OLDER
         echo file !FileName! !Original! is older then file !FileNameCompare! !Compare!   
   )
)


But then the outcome is like this
file e:\Pandoraflyff\Resclient\Flyff.a is Older then e:\Pandoraflyff\Patch\Flyff.a.gz
file e:\Pandoraflyff\Resclient\Flyff.a is Older then e:\Pandoraflyff\Patch\Neuz.exe.gz

So its checking 1 file in folder 1 to all files in folder2.

julesverne
Posts: 81
Joined: 19 Nov 2013 00:41

Re: File Comparison by Date Time

#9 Post by julesverne » 26 May 2014 03:43

julesverne wrote:I missed some data you stated in your original post so I edited my first reply above. Make that change and try it again.


Whoops... edited again, because my edit needs to be reversed. "%%A.gz" EQU "%%B"

imagination
Posts: 13
Joined: 24 May 2014 01:32

Re: File Comparison by Date Time

#10 Post by imagination » 26 May 2014 03:47

Well that didnt worked also

Here is an image taken of the outcome

Image

As you can see its checking 1 file to all files

Thats with this code

Code: Select all

@echo off
SETLOCAL EnableDelayedExpansion

REM GETTING FILES IN RESCLIENT
FOR /R e:\Pandoraflyff\Resclient %%A IN (*.*) do (
   SET Original=%%~TA
   SET FileName=%%A
   FOR /R e:\Pandoraflyff\Patcher %%B IN (*.*) do (
         SET Compare=%%~TB
         SET FileNameCompare=%%B
         if "%%A" EQU "%%B.gz" if !Original! LSS !Compare! ( Call OLDER ) ELSE ( Call NEWER )
      
       :OLDER
      echo file !FileName! !Original! is older then file !FileNameCompare! !Compare!
   )
)

REM :OLDER
REM echo file !FileName! !Original! is older then file !FileNameCompare! !Compare!

goto :eof
:NEWER
echo !Original! !Compare!
goto :eof


If i use your code and i dont change it
then i am only getting 1 result

So i think the for loop isnt correct but i cant figure it out.

Dragokas
Posts: 43
Joined: 30 Jul 2013 09:42
Location: Ukraine, USSR
Contact:

Re: File Comparison by Date Time

#11 Post by Dragokas » 26 May 2014 03:50

Hi, imagination !

You can use this:

Code: Select all

robocopy "%folder1%" "%folder2%" *.* /E /IS /XL /L /NJS /NJH /NDL


There is no copy operation (just listing).
For more functionality use piping to 'find' or simply parsing with "tokens=1,2*".
You can add /TS switch for wieving the date/time of original file.
'robocopy' utility exists in OS starting with Vista+.

Best regards, Alex.

imagination
Posts: 13
Joined: 24 May 2014 01:32

Re: File Comparison by Date Time

#12 Post by imagination » 26 May 2014 04:07

Dragokas wrote:Hi, imagination !

You can use this:

Code: Select all

robocopy "%folder1%" "%folder2%" *.* /E /IS /XL /L /NJS /NJH /NDL


There is no copy operation (just listing).
For more functionality use piping to 'find' or simply parsing with "tokens=1,2*".
You can add /TS switch for wieving the date/time of original file.
'robocopy' utility exists in OS starting with Vista+.

Best regards, Alex.



Thanks but if i read the manual it only copys the original file name.
Sure it can compare
But i dont think its fit for my job.

Example
if file in folder1 is newer then file in folder2 then he must call Gzip for gzipping the correct file.
i dont think it can be done with robocopy cause its job is only to copy.

If i am wrong then tell me xD

With kind regards.

Aacini
Expert
Posts: 1889
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: File Comparison by Date Time

#13 Post by Aacini » 26 May 2014 04:24

I think you want not to compare "a list of files in 1 dir to a list of files in an other dir". If you want to "review a list of files in 1 dir, and compare each file vs. the file with same name in another dir", then the code below do that:

EDIT: I reviewed your examples and I missed the fact that you want to compare the first file vs. the second one located in the same path (excepting for the starter paths).

Code: Select all

@echo off
SETLOCAL EnableDelayedExpansion

REM GETTING FILES IN RESCLIENT
FOR /R e:\Pandoraflyff\Resclient %%A IN (*.*) do (
   SET Original=%%~TA
   SET FileName=%%A

   REM COMPARE THIS FILE VS. THE CORRESPONDING ONE (SAME PATH) PATCHER
   FOR %%B IN ("!FileName:Resclient=Patcher!.GZ") do (
      SET Compare=%%~TB
      SET FileNameCompare=%%B
      if !Original! LSS !Compare! ( CALL :OLDER ) ELSE ( CALL :NEWER )
   )

)
GOTO :EOF

:OLDER
echo file !FileName! !Original! is older then file !FileNameCompare! !Compare!
exit /B

:NEWER
echo !Original! !Compare!
exit /B


Anyway, you can NOT directly compare the value given by %%~T in order to know which file is older/newer; you must transform both values to a standard YYYYMMDDHHMMSS string.

Antonio
Last edited by Aacini on 26 May 2014 04:51, edited 1 time in total.

imagination
Posts: 13
Joined: 24 May 2014 01:32

Re: File Comparison by Date Time

#14 Post by imagination » 26 May 2014 04:51

Aacini wrote:I think you want not to compare "a list of files in 1 dir to a list of files in an other dir". If you want to "review a list of files in 1 dir, and compare each file vs. the file with same name in another dir", then the code below do that:

Code: Select all

@echo off
SETLOCAL EnableDelayedExpansion

REM GETTING FILES IN RESCLIENT
FOR /R e:\Pandoraflyff\Resclient %%A IN (*.*) do (
   SET Original=%%~TA
   SET FileName=%%A

   REM COMPARE THIS FILE VS. THE CORRESPONDING ONE IN PATCHER
   FOR %%B IN ("e:\Pandoraflyff\Patcher\%%~NXA.GZ") do (
      SET Compare=%%~TB
      SET FileNameCompare=%%B
      if !Original! LSS !Compare! ( CALL :OLDER ) ELSE ( CALL :NEWER )
   )

)
GOTO :EOF

:OLDER
echo file !FileName! !Original! is older then file !FileNameCompare! !Compare!
exit /B

:NEWER
echo !Original! !Compare!
exit /B


Anyway, you can NOT directly compare the value given by %%~T in order to know which file is older/newer; you must transform both values to a standard YYYYMMDDHHMMSS string.

Antonio


The %%~T is already giving out DD-MM-YYYY HH:MM on both files. so i dont have to convert it to YYYY-MM-DD
The code you gave is also not outputting the desired result

as you can see in the image
Image

It compares all the files of e:\pandoraflyff\resclient(\subfolders) to all files only in e:\Pandoraflyff\Patcher

Aacini
Expert
Posts: 1889
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: File Comparison by Date Time

#15 Post by Aacini » 26 May 2014 04:52

See the edit in my code above. If the last code is not what you want, please specify exactly what do you want in plain English.

EDIT:
imagination wrote:The %%~T is already giving out DD-MM-YYYY HH:MM on both files. so i dont have to convert it to YYYY-MM-DD


Suppose that file1 was created on 20-04-2014 and file2 on 01-05-2014. The direct comparison of these dates said that the first one is newer than the second one...

Antonio

Post Reply