|
-
Dec 7th, 2001, 02:12 AM
#1
Thread Starter
Addicted Member
Compare files
Hello there,
I want to compare files to find out if they are the same (except for the name of course).
In my program I can specify a directory. Then I want to check whether there are any redundant files. My app works well with directories that contain less than 300 files. Once above it becomes very slow. I once tried to check a directory with 12000 files and it was S-L-O-W.
Currently I open the files for Binary as ...
then I compare them.
12000 files means (in my case) +- 72 million comparisons...
My question is if there is a quicker way to do this. Can I use an API? Or is there a single command that compares two files??
Please help!
-
Dec 7th, 2001, 03:47 AM
#2
To the best of my knowledge there is no such function.
The only way I can think of is to read the files for binary into a byte array and do the comparing.
This, I assume, doesn't differ from how you do it.
But how do you do the actual comparasing?
First of all you don't have to read the files if they differ in size so you should check that first.
Then if they are of the same size and you've read two files into two byte arrays use the StrComp function to compare them.
VB Code:
' b1 and b2 are two byte arrays
If StrComp(b1, b2) = 0 Then
MsgBox "The files are the same"
End If
Best regards
-
Dec 7th, 2001, 04:38 AM
#3
Thread Starter
Addicted Member
I can try this but eeeh...
How to use a byte array? Do you have an example?
-
Dec 7th, 2001, 06:36 AM
#4
Hyperactive Member
a byte array is a lot like a string except it holds single bytes, thus you need the array to hold more then one. To use one you would dim the array first then when u know the length of the file, redim the array.
but i dunno any advantage of a byte array over a regular string
and comparing those
VB Code:
Dim vFile1 As String,vFile2 As String
Open FileName1 For Binary As #1
vFile1 = Space(LOF(1))
Get #1, 1, vFile1
Close #1
Open FileName2 For Binary As #1
vFile2 = Space(LOF(1))
Get #1, 1, vFile2
Close #1
If StrComp(vFile1, vFile2) = 0 Then
MsgBox "The files are the same"
End If
Last edited by Eyes.Only; Dec 7th, 2001 at 06:42 AM.
-
Dec 7th, 2001, 06:51 AM
#5
Thread Starter
Addicted Member
Thx,
I will try this
-
Dec 7th, 2001, 07:02 AM
#6
Hyperactive Member
also in case u wanted to look at how somebody else did it check this site out,
Compare Files
They used a similar way, but only collected 10,000 byte chunks at a time for comparing...-shrug-
-
Dec 7th, 2001, 11:09 AM
#7
Originally posted by Eyes.Only
a byte array is a lot like a string except it holds single bytes, thus you need the array to hold more then one. To use one you would dim the array first then when u know the length of the file, redim the array.
but i dunno any advantage of a byte array over a regular string
and comparing those
A string takes up twice as much memory as the byte array.
VB Code:
Dim b1() As Byte, b2() As Byte
Open FileName1 For Binary As #1
ReDim b1(LOF(1) - 1) 'we take of one since the array is zero based
Get #1, , b1
Close #1
Open FileName2 For Binary As #1
ReDim b2(LOF(1) - 1)
Get #1, , b2
Close #1
If StrComp(b1, b2) = 0 Then
MsgBox "The files are the same"
End If
Best regards
-
Dec 7th, 2001, 11:23 AM
#8
Member
Well first off... There is no need to compare the files if their file size doesn't match. From what I see in the examples, it checks the files regardless.
Also you could compare only the first 32 bytes of a file. If the file sizes are the same AND the first 32 bytes are the same THEN you could compare the whole file.
-
Dec 7th, 2001, 11:37 AM
#9
Originally posted by EternalKnight
Well first off... There is no need to compare the files if their file size doesn't match. From what I see in the examples, it checks the files regardless.
The examples is for doing the file comparing, but if you read my first answer you'll see that I mentioned this.
-
Dec 7th, 2001, 11:43 AM
#10
Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|