Bat file help

Sureshot324

Diamond Member
Feb 4, 2003
3,370
0
71
I'm writing a bat file that copies some files from one place to another. I want it to overwrite any duplicates in the destination unless the files are completely identical. How would I check that the files are identical in Dos?

It would be good enough to verify that the file sizes are exactly identical, but how would I check the size of the files?
 

OogyWaWa

Senior member
Jan 20, 2009
623
0
71
i suppose checking the file size down to the byte would be close enough...

some brief googling found this: http://www.ericphelps.com/batch/samples/filesize.txt
didn't read it but it might help

also, you could check the hash of the file, but that could take a while depending on size, etc. not sure how to do it with a basic batch file though.

you could just write a small shell script or program to do it. might be easier. then you could add fancy dancy things like a status bar and error messages :D
 

Billb2

Diamond Member
Mar 25, 2005
3,035
70
86
Load the result of dir:filename into edlin. use edlin to pull out the size.
 

Snapster

Diamond Member
Oct 14, 2001
3,916
0
0
Isn't this just re-inventing the wheel, unless you are trying to exact to the binary level.

There are examples out there that already do this and you can even do it with the xcopy command with differential switch.

 

Sureshot324

Diamond Member
Feb 4, 2003
3,370
0
71
My script copies mp3 files, and I don't want it to waste time by overwriting files with identical ones. However, if I make a small change like update the ID3 tag, then I do want it to overwrite. If I use something like fc I having a feeling it would take just as long to compare the files as it would to just overwrite them. That's why I wanted to use file sizes to compare, but I'd be open to any alternative that is fast.

I googled xcopy and couldn't find a way to do this, except maybe compare last modified dates. What is the command line switch to check if the files are identical? Btw apparently xcopy has been deprecated in favor of robocopy.

I found out that %~z1 will return the file size in bytes of of argument 1. However, this doesn't seem to work with the variable in a for loop. For example

for /f "delims=;" %%a in ('dir /b') do (
echo %%~za
)

This doesn't work. It only seems to work for command line arguments, like %~z1, %~z2, etc...

I'm gonna try Billb2's solution next, though it is a little awkward.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
The xcopy utility will just check the file time, I believe. It has an "overwrite if newer" switch (/D), but you're really talking about "overwrite if different." The only really reliable way to detect whether a file is identical, other than a byte-wise comparison, is to generate an MD5 hash for each and compare them. If they hash the same they're identical.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,700
4,661
75
That particular for loop crashes my command prompt! But it seems equivalent to this much simpler one:

for %%a in (*) do (
echo %%~za
)

This kind of thing works at the command prompt for me to compare GVim backup files (which are like the original, but end with a "~"):

for %i in (*) do for %j in (%i~) do if exist %j echo %i %~zi %~zj

Does that help?
 

Sureshot324

Diamond Member
Feb 4, 2003
3,370
0
71
Originally posted by: VinylxScratches
Where are you copying files from and to?

The script is to sync my music collection on my home computer and an external hard drive which I connect to my car stereo. The folder hierarchy needs to be somewhat different for easier browsing on the crappy car stereo interface, hence the need for a script.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: Markbnj
The xcopy utility will just check the file time, I believe. It has an "overwrite if newer" switch (/D), but you're really talking about "overwrite if different." The only really reliable way to detect whether a file is identical, other than a byte-wise comparison, is to generate an MD5 hash for each and compare them. If they hash the same they're identical.

Given two files of the same size, its best to just compare byte-by-byte (smarter: 8 bytes at a time), rather than do an MD5. MD5 is a lot more computationally intensive than a comparison. If you have N files, however, you can do N MD5s, then do an N^2 comparison over the hashes.

There's also a (very small -- almost statistically irrelevant) chance that two non-identical files will hash to the same value. That chance goes up with weaker/shorter hashes (e.g. checksums).
 

Sureshot324

Diamond Member
Feb 4, 2003
3,370
0
71
Originally posted by: Ken g6
That particular for loop crashes my command prompt! But it seems equivalent to this much simpler one:

for %%a in (*) do (
echo %%~za
)

This kind of thing works at the command prompt for me to compare GVim backup files (which are like the original, but end with a "~"):

for %i in (*) do for %j in (%i~) do if exist %j echo %i %~zi %~zj

Does that help?

That works. Didn't know you could just put a * in the for loop instead of a dir command. Still not sure why my version didn't work though.

I ended up finding out that the size of mp3 files doesn't change when you edit the id3 tag. It reserves a certain amount of space for them no matter what. I am now using the xcopy command with the /D switch to check last modified date.

Thanks for the help everyone!