Page 1 of 1

Compare date last modified of two files

Posted: 19 Oct 2010 22:04
by DOSScriptWriter
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

Re: Compare date last modified of two files

Posted: 19 Oct 2010 22:54
by ghostmachine4
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

Re: Compare date last modified of two files

Posted: 19 Oct 2010 23:17
by amel27
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"

Re: Compare date last modified of two files

Posted: 19 Oct 2010 23:27
by ghostmachine4
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.

Re: Compare date last modified of two files

Posted: 25 Oct 2010 19:54
by amel27

Code: Select all

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

Re: Compare date last modified of two files

Posted: 09 Apr 2011 21:04
by DOSScriptWriter
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

Re: Compare date last modified of two files

Posted: 10 Apr 2011 07:36
by aGerman
Remove the /f switches in both for loops.

Regards
aGerman

Re: Compare date last modified of two files

Posted: 10 Apr 2011 09:11
by DOSScriptWriter
I did. Still won't work :(

Re: Compare date last modified of two files

Posted: 10 Apr 2011 09:29
by aGerman
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

Re: Compare date last modified of two files

Posted: 10 Apr 2011 14:23
by DOSScriptWriter
Done. Thanks so much. Appreciate everybody's help.

Re: Compare date last modified of two files

Posted: 10 Apr 2011 15:47
by aGerman
Credits have to go to amel27 who suggested this first (in the third post) :wink:

Regards
aGerman