Compare date last modified of two files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DOSScriptWriter
Posts: 12
Joined: 18 Oct 2010 14:51

Compare date last modified of two files

#1 Post by DOSScriptWriter » 19 Oct 2010 22:04

I have two files with the same name, let's say "C:\file.txt" and "D:\file.txt". I need a script which when I run it, will compare date last modified for the two and write file.txt from C to D if they are not the same.

Thanks

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Compare date last modified of two files

#2 Post by ghostmachine4 » 19 Oct 2010 22:54

vbscript for dealing with dates

Code: Select all

strFile1 = WScript.Arguments(0)
strFile2 = WScript.Arguments(1)
Set objFS = CreateObject( "Scripting.FileSystemObject" )
If objFS.GetFile(strFile1).DateLastModified > objFS.GetFile(strFile2).DateLastModified Then
   WScript.Echo strFile1 & " newer than " & strFile2
Else
   WScript.Echo strFile2 & " newer than " & strFile1
End If



usage

Code: Select all

C:\test> cscript //nologo myscript.vbs C:\file.txt D:\file.txt

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Compare date last modified of two files

#3 Post by amel27 » 19 Oct 2010 23:17

by time:

Code: Select all

for /f %%a in ("C:\file.txt") do for /f %%b in ("D:\file.txt") do (
if not "%%~ta"=="%%~tb" copy /y "C:\file.txt" "D:\file.txt"
)
by content:

Code: Select all

fc "C:\file.txt" "D:\file.txt" >nul||copy /y "C:\file.txt" "D:\file.txt"

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Compare date last modified of two files

#4 Post by ghostmachine4 » 19 Oct 2010 23:27

amel27 wrote:by time:

Code: Select all

for /f %%a in ("C:\file.txt") do for /f %%b in ("D:\file.txt") do (
if not "%%~ta"=="%%~tb" copy /y "C:\file.txt" "D:\file.txt"
)
by content:

Code: Select all

fc "C:\file.txt" "D:\file.txt" >nul||copy /y "C:\file.txt" "D:\file.txt"

Using %%~ta will not be accurate, since it doesn't display number of seconds. So even though 2 files may be the same until the last minute, they will not be the same if the number of seconds differ.

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Compare date last modified of two files

#5 Post by amel27 » 25 Oct 2010 19:54

Code: Select all

xcopy /d /y "C:\file.txt" "D:\file.txt"

DOSScriptWriter
Posts: 12
Joined: 18 Oct 2010 14:51

Re: Compare date last modified of two files

#6 Post by DOSScriptWriter » 09 Apr 2011 21:04

Okay I tried the code below:

for /f %%a in ("C:\New Folder1\file.txt") do for /f %%b in ("C:\New Folder2\file.txt") do (
if not "%%~ta"=="%%~tb" copy /y "C:\New Folder1\file.txt" "C:\New Folder2\file.txt"
)

but could not get it to work. Don't know why. Nothing happens and no errors either - just blank :x Is there another way of accomplishing it? I am now using "C:\NewFolder1\file.txt" and "C:\NewFolder2\file.txt".


Thanks

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

Re: Compare date last modified of two files

#7 Post by aGerman » 10 Apr 2011 07:36

Remove the /f switches in both for loops.

Regards
aGerman

DOSScriptWriter
Posts: 12
Joined: 18 Oct 2010 14:51

Re: Compare date last modified of two files

#8 Post by DOSScriptWriter » 10 Apr 2011 09:11

I did. Still won't work :(

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

Re: Compare date last modified of two files

#9 Post by aGerman » 10 Apr 2011 09:29

Hmm, works for me :?

BTW A different "date last modified" is an indication that a files content was changed. But actually the file could have the same content even if the date is not the same and of course two files could be different even if they have the same date.
Maybe you should change your strategy. FC would compare the files. It returns errorlevel 1 if they are not equal.

Code: Select all

fc /b "C:\New Folder1\file.txt" "C:\New Folder2\file.txt" >nul ||copy /y "C:\New Folder1\file.txt" "C:\New Folder2\file.txt"

Regards
aGerman

DOSScriptWriter
Posts: 12
Joined: 18 Oct 2010 14:51

Re: Compare date last modified of two files

#10 Post by DOSScriptWriter » 10 Apr 2011 14:23

Done. Thanks so much. Appreciate everybody's help.

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

Re: Compare date last modified of two files

#11 Post by aGerman » 10 Apr 2011 15:47

Credits have to go to amel27 who suggested this first (in the third post) :wink:

Regards
aGerman

Post Reply