Compare date last modified of two files
Moderator: DosItHelp
-
- Posts: 12
- Joined: 18 Oct 2010 14:51
Compare date last modified of two files
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
Thanks
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Compare date last modified of two files
vbscript for dealing with dates
usage
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
by time:by content:
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"
)
Code: Select all
fc "C:\file.txt" "D:\file.txt" >nul||copy /y "C:\file.txt" "D:\file.txt"
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Compare date last modified of two files
amel27 wrote:by time:by content: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"
)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
Code: Select all
xcopy /d /y "C:\file.txt" "D:\file.txt"
-
- Posts: 12
- Joined: 18 Oct 2010 14:51
Re: Compare date last modified of two files
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 Is there another way of accomplishing it? I am now using "C:\NewFolder1\file.txt" and "C:\NewFolder2\file.txt".
Thanks
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 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
Remove the /f switches in both for loops.
Regards
aGerman
Regards
aGerman
-
- Posts: 12
- Joined: 18 Oct 2010 14:51
Re: Compare date last modified of two files
I did. Still won't work
Re: Compare date last modified of two files
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.
Regards
aGerman
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
-
- Posts: 12
- Joined: 18 Oct 2010 14:51
Re: Compare date last modified of two files
Done. Thanks so much. Appreciate everybody's help.
Re: Compare date last modified of two files
Credits have to go to amel27 who suggested this first (in the third post)
Regards
aGerman
Regards
aGerman