Results 1 to 10 of 10

Thread: Compare files

  1. #1

    Thread Starter
    Addicted Member chrisvl's Avatar
    Join Date
    Jul 2001
    Location
    Belgium - Somewhere between the bedroom and the toilet...or at work playing Oracle DBA :-)
    Posts
    233

    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!

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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:
    1. ' b1 and b2 are two byte arrays
    2. If StrComp(b1, b2) = 0 Then
    3.     MsgBox "The files are the same"
    4. End If
    Best regards

  3. #3

    Thread Starter
    Addicted Member chrisvl's Avatar
    Join Date
    Jul 2001
    Location
    Belgium - Somewhere between the bedroom and the toilet...or at work playing Oracle DBA :-)
    Posts
    233
    I can try this but eeeh...

    How to use a byte array? Do you have an example?

  4. #4
    Hyperactive Member Eyes.Only's Avatar
    Join Date
    Oct 2001
    Location
    Minnesota
    Posts
    347
    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:
    1. Dim vFile1 As String,vFile2 As String
    2.  
    3. Open FileName1 For Binary As #1
    4. vFile1 = Space(LOF(1))
    5. Get #1, 1, vFile1
    6. Close #1
    7.  
    8. Open FileName2 For Binary As #1
    9. vFile2 = Space(LOF(1))
    10. Get #1, 1, vFile2
    11. Close #1
    12.  
    13. If StrComp(vFile1, vFile2) = 0 Then
    14.     MsgBox "The files are the same"
    15. End If
    Last edited by Eyes.Only; Dec 7th, 2001 at 06:42 AM.

  5. #5

    Thread Starter
    Addicted Member chrisvl's Avatar
    Join Date
    Jul 2001
    Location
    Belgium - Somewhere between the bedroom and the toilet...or at work playing Oracle DBA :-)
    Posts
    233
    Thx,

    I will try this

  6. #6
    Hyperactive Member Eyes.Only's Avatar
    Join Date
    Oct 2001
    Location
    Minnesota
    Posts
    347
    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-

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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:
    1. Dim b1() As Byte, b2() As Byte
    2.  
    3. Open FileName1 For Binary As #1
    4. ReDim b1(LOF(1) - 1) 'we take of one since the array is zero based
    5. Get #1, , b1
    6. Close #1
    7.  
    8. Open FileName2 For Binary As #1
    9. ReDim b2(LOF(1) - 1)
    10. Get #1, , b2
    11. Close #1
    12.  
    13. If StrComp(b1, b2) = 0 Then
    14.     MsgBox "The files are the same"
    15. End If
    Best regards

  8. #8
    Member EternalKnight's Avatar
    Join Date
    Apr 2001
    Posts
    53
    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.

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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.

  10. #10
    Member EternalKnight's Avatar
    Join Date
    Apr 2001
    Posts
    53
    sorry, just noticed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width