Dos Batch to compare file times

Lenovo / W500
January 8, 2009 at 06:28:25
Specs: Windows XP, 2GB
I want to write a batch script which execute a command only if one file is newer than another.

(I am running a program which will modify the 2nd file unless the user clicks cancel. I only want to run the second program if the first program is completed and the file is rewritten - it may or may not have actually been changed - but the file time will have changed.)

Can you help?


See More: Dos Batch to compare file times

Report •


#1
January 8, 2009 at 06:44:49
It is VERY difficult to compare for "more recent" using cmd.exe.

On the other hand, it is very EASY to compare the file times to see if the are the same or different. You just can't tell which one came first.

Are you comparing the file's timestamp with that of a copy of the original file? If so, then just compare timestamps for equality.

RunProgram1.exe
for %%a in (file1.dat) do for %%b in ("Copy of file1.dat") do if not "%%~ta" == "%%~tb" then RunProgram2.exe

CAVEAT: The batch file returns timestamps only to minute precision. If you require timestamps to second (or finer) precision, you need to use another language.


Report •

#2
January 8, 2009 at 07:06:05
Thanks for the prompt reply. I really appreciate your help.

I have been playing with this while waiting for a reply but haven't found a good solution.

However, the one minute restriction could be a problem. In many cases the user clicks OK on program1.exe in less than 1 minute.

1. Can I set the time on the file to an earlier (or completely bogus) time before running Program1.exe? Then the minute problem would not be a problem because the file time would have changed.

2. It turns out that I can put some dummy data at the end of the file before running Program1.exe, and if a new file is created it will not have the dummy data.

Is there a way to compare the files without using find.exe? (on my own machine there is another version of Find.exe earler in the path). Does FC return an error level status? Or is there another way to compare two files?


Report •

#3
January 8, 2009 at 08:15:01
copy myfile pre
program1.exe myfile
fc myfile pre > nul
if errorlevel 1 @echo changed


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions

#4
January 8, 2009 at 08:42:43
Thanks, the FC works quite well for me.

Report •

#5
January 8, 2009 at 11:05:43
Here's a faster version that doesn't need dummy data to be added to the file. It relies on the XCOPY /D feature which only copies the source file if it is newer than the destination file. Since we don't actually want to copy, but just compare file stamps, we set the destination file to read-only. We then check XCOPY's errorlevel - if it has failed, it means the file was modified by your first program and it tried and failed to copy the file.

rem Run program1.exe on file1.dat. If, and
rem only if, program1.exe modifies file1.dat, run program2.exe.
rem
rem Create an empty file with current datestamp.
type nul > tmp.tmp
program1.exe file1.dat
rem Check if file1.dat is newer than tmp.tmp
attrib +r tmp.tmp
xcopy /y /d file1.dat tmp.tmp >nul 2>&1
rem Store the result
if errorlevel 1 (set FileModified=1) else (set FileModified=)
rem Clean up before running program2.
attrib -r tmp.tmp
del tmp.tmp
if defined FileModified program2.exe


Report •

#6
January 8, 2009 at 12:27:25
xcopy /L/d myfile dest\


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#7
January 8, 2009 at 12:42:53
Where does this last response:

xcopy /L/d myfile dest\

fit into the whole script:

copy myfile pre
program1.exe myfile
fc myfile pre > nul
if errorlevel 1 @echo changed


Report •

#8
January 8, 2009 at 15:12:50
M2, xcopy /L can be useful if you want to see if it would copy a file (rather than actually copying it) but it does not help us here, as it always returns errorlevel 0.

Report •

#9
January 8, 2009 at 19:39:22
using m2 script:
echo n|xcopy /L/d myfile dest\|find "myfile"
if not errorlevel 1 echo changed


Report •

#10
January 9, 2009 at 01:32:33
I like that, reno. In fact, since you are answering "n" to xcopy, you don't need the /L. So, putting it all together:

type nul > tmp.tmp
program1.exe file1.dat
echo n|xcopy /d file1.dat tmp.tmp|find "Overwrite">nul
if errorlevel 1 (
del tmp.tmp
) else (
del tmp.tmp
program2.exe
)


Report •

#11
January 9, 2009 at 07:31:01
Good job all. Thanks for thinking about this for me,

The only problem with the last solution is that it uses Find.exe and I have Find.exe on my machine replaced by a MKS (Unix like) version.

I wound up using:

echo X >> file1
copy file1 file2
program1.exe file1
fc file1 file2 > nul
if errorlevel 1 program2

Adding the dummy character to the original file does not create a problem in my situation.

Again, thanks for all the help


Report •

#12
January 9, 2009 at 09:48:03
Since that works, all's well. For future reference, if you need the functionality of Windows' find.exe, you can use the alternative utility, findstr.exe - it has a different syntax but it's more powerful. Type findstr /? for details. But then if you've got MKS, you probably also have grep on your system.

Report •


Ask Question