Link to home
Start Free TrialLog in
Avatar of MCofer
MCofer

asked on

How to Compare files in two directories.

my First attempt simply used the last modifided date from FileInfo.
The problem I am having is one set of files were installed using InstallShield and for some reason the modification times vary by a second or two on copies of the exact same file.
Is there an better (Quick) way to compare files. I don't want to have to open and read the files. This needs to be fast.
Avatar of strickdd
strickdd
Flag of United States of America image

If you are just trying to write a utility to do this here is a pretty good one: http://www.grigsoft.com/wincmp.htm.

If you need it in your code, you can:


Dim stream1 As New System.IO.StreamReader(Path)
        Dim stream2 As New System.IO.StreamReader(Path2)

        Dim string1 As String = stream1.ReadToEnd()
        Dim string2 As String = stream2.ReadToEnd()

        If (string1 = string2) Then
            'they are equal
        Else
            'they are different
        End If
Avatar of ctm5
ctm5

The only way I have found is to throw each into a stream reader, read into a string, and then see if the strings are equal. You can turn on Option Compare Binary to speed up reading the files.

I know you said you didn't want to have to open and read the files, but I searched for quite a long time trying to find a different way, and had no luck. Anyone?

ctm5
oooh, strickdd, we came to the same conclusion!

ctm5
Yeah, you were just a minure behind:)
*minute
Avatar of MCofer

ASKER

My problem is one set of files is on the users local machine and the other is on the network (VPN).
The users are scatterted all over the world and are often on very slow connections.
My idea was to create an XML file(updated each time a file changes) with  fileInfo.LastWriteTime so the user did not actually need to search the network server.
Is it possible to write a hash or maybe file checksum to an XML file. Opening each nework file just takes to long.
ASKER CERTIFIED SOLUTION
Avatar of strickdd
strickdd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So you can do what you want -- store the hash in an XML file on the server. It will have to be updated everytime someone changes the file, of course, and you could run into concurrancy issues.

ctm5
Why are you comparing the files? Are you trying to keep everyone in sync? Could you possibly use ClickOnce deployment for these files and let that take care of the versioning?

David
Another option you have is to run: http://www.download.com/XCopy-Net/3000-2248_4-10483581.html?tag=lst-0-2. You can make sure that the newest file is in a specific location. It might be worth your trouble. It is a little buggy when saving your settings, but otherwise it is pretty quick, efficient, and FREE
Avatar of MCofer

ASKER

Thanks to everyone for the help
The XCopy freeware was close to what I actually needed.
The Hash would be to slow to use over the network but storing the hash in an XML worked fine.

Again Thanks