Results 1 to 3 of 3

Thread: What is the fastest way to tell if one file is equal to another?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269

    What is the fastest way to tell if one file is equal to another?

    Anyone know?
    Id assume the first check would be to just see if the filesizes are the same, and if they are.. then what? Should I do a CRC32 checksum? Or just load the data of the files into 2 strings and see if they are the same? What is the fastest way to do so?

  2. #2
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: What is the fastest way to tell if one file is equal to another?

    If you load in a string, since a string is unicode, you take twice the memory of the file, and since you open 2 files, you take quite a lot of memory....

    You should load in a byte array, like this:
    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Dim File1() As Byte
        Dim File2() As Byte
        Dim K As Long
        
        Open "C:\File1.txt" For Binary Access Read As #1
        Open "C:\File2.txt" For Binary Access Read As #2
            ReDim File1(LOF(1) - 1)
            ReDim File2(LOF(2) - 1)
            
            Get #1, , File1
            Get #2, , File2
        Close #1, #2
        
        For K = 0 To UBound(File1)
            If File1(K) <> File2(K) Then Exit For
        Next K
        
        If K = UBound(File1) + 1 Then
            MsgBox "Files contain the same data", vbInformation
        Else
            MsgBox "Files are not the same at position: " & K, vbInformation
        End If
    End Sub
    And if the files are very big, then it would be better to load chunks at a time instead of the whole file (if you don't know how to do that I can write a short example for you)

  3. #3
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: What is the fastest way to tell if one file is equal to another?

    CVMichael aid, "And if the files are very big, then it would be better to load chunks at a time instead of the whole file (if you don't know how to do that I can write a short example for you)."
    --------------------
    I tried your code using a matched pair of 3.3 Mb Rtf files. I'm not sure what your definition of "very big" is, but the code ran with these two files in about a millisecond or so on my 1.7 GHz machine. I didn't use a timer control to check it, but my eyes don't blink much faster.
    Doctor Ed

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