Computing.Net > Forums > Programming > Dos Batch to compare file times

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Dos Batch to compare file times

Reply to Message Icon

Name: alh-aritek
Date: January 8, 2009 at 06:28:25 Pacific
OS: Windows XP
CPU/Ram: 2GB
Product: Lenovo / W500
Subcategory: Batch
Comment:

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?



Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: January 8, 2009 at 06:44:49 Pacific
Reply:

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.


0

Response Number 2
Name: alh-aritek
Date: January 8, 2009 at 07:06:05 Pacific
Reply:

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?


0

Response Number 3
Name: Mechanix2Go
Date: January 8, 2009 at 08:15:01 Pacific
Reply:

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


0

Response Number 4
Name: alh-aritek
Date: January 8, 2009 at 08:42:43 Pacific
Reply:

Thanks, the FC works quite well for me.


0

Response Number 5
Name: klint
Date: January 8, 2009 at 11:05:43 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: January 8, 2009 at 12:27:25 Pacific
Reply:

xcopy /L/d myfile dest\


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

M2


0

Response Number 7
Name: alh-aritek
Date: January 8, 2009 at 12:42:53 Pacific
Reply:

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


0

Response Number 8
Name: klint
Date: January 8, 2009 at 15:12:50 Pacific
Reply:

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.


0

Response Number 9
Name: reno
Date: January 8, 2009 at 19:39:22 Pacific
Reply:

using m2 script:
echo n|xcopy /L/d myfile dest\|find "myfile"
if not errorlevel 1 echo changed


0

Response Number 10
Name: klint
Date: January 9, 2009 at 01:32:33 Pacific
Reply:

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
)


0

Response Number 11
Name: alh-aritek
Date: January 9, 2009 at 07:31:01 Pacific
Reply:

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


0

Response Number 12
Name: klint
Date: January 9, 2009 at 09:48:03 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Dos Batch to compare file times

Batch to compare files' size www.computing.net/answers/programming/batch-to-compare-files-size/17428.html

DOS batch to move files of certain size www.computing.net/answers/programming/dos-batch-to-move-files-of-certain-size/20194.html

Batch to compare and move files www.computing.net/answers/programming/batch-to-compare-and-move-files/13491.html