Compare 2 files modified dates

Microsoft Windows xp home edition with s...
November 5, 2010 at 21:20:03
Specs: Windows XP
I am writing a batch file in Windows XP that will compare two files to see which one was most recently modified and copy that file to a directory. Sounds easy right, here's the catch. Both of these files have the same file name so they can't be compared by putting them in the same directory and using a command such as (for /f "tokens=*" %%a in ('dir /b /od') do set lastmod=%%a).

Does anyone know of a way to extract the date modified from a specific file and set it to a variable? If I could do this, I could compare the two dates and copy the one that was updated most recently.This is the only way I can think that it would be possible. Would this work or is there another way?

Thanks!!!


See More: Compare 2 files modified dates

Report •

#1
November 6, 2010 at 03:41:45
If one is in the dir above:

====================================

@echo off & setLocal EnableDELAYedeXpansion

copy ..\myfile #

for /f "tokens=* delims= " %%a in ('dir/b/o-d # myfile') do (
echo copy %%a d:\
goto :done
)
:done
del #


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
November 6, 2010 at 03:59:11
Since the COPY command doesn't modify the file timestamp, the following batch does the job

@echo off & setlocal EnableDelayedExpansion

set FileName=The File To Be Copied
set dirA=First Directory
set dirB=Second Directory
set TargetDir=Destination Directory

md \MyTemp
pushd \MyTemp
copy "%dirA%\%FileName%" File.A > nul
copy "%dirB%\%FileName%" File.B > nul
for /F %%j in ('dir /B /OD File.?') do set lastmod=%%~xj
set lastmod=%lastmod:~-1%
popd
rd /Q /S \MyTemp

copy "!dir%lastmod%!\%FileName%" "%TargetDir%"
:: End_Of_Batch


Report •

#3
November 6, 2010 at 08:53:04
Thanks Mechanix2Go and IVO for the code but this still doesn't solve my problem. I just realized that you can't have 2 files with the same file name in the same folder so neither of these solutions solve my problem. And even if you could have them both in the same folder, there is no way to tell the difference between the two once you have figured out which one was most recently modified.

The only solution I can think of, is to set the the date modified for both of these files to a variable and compare the two dates. Does anyone know of any way to do this because I have been searching around the internet and I can't find any way to do this.

Thanks!!!


Report •

Related Solutions

#4
November 6, 2010 at 10:24:43
I don't understand what you are saying.

My script does the job copying to the destination folder the most recently modified file comparing two files with the same name stored in different folders. I tested it and the script worked perfectly. It may be streamlined but that is an improvement.

Try to believe. By the way comparing two dates is not easy in Windows batch.


Report •

#5
November 7, 2010 at 00:44:45
LOL No tellin' what's going on. Mine was also tested.

Screen shot:

====================================
C:\temp\-\proxy\subs\UN\xxx>lastmod
1 file(s) copied.
copy MYFILE d:\

C:\temp\-\proxy\subs\UN\xxx>edit ..\myfile

C:\temp\-\proxy\subs\UN\xxx>lastmod
1 file(s) copied.
copy # d:\

C:\temp\-\proxy\subs\UN\xxx>


=====================================
Life is too important to be taken seriously.

M2


Report •

Ask Question