Visual Basic Tip: 04/08/96

Compare Two Files.

The code listed below compares the contents of two files.  The
result is a determination whether the files have identical content.
This example used only Visual Basic code.

Other ideas:  Add a progress indicator (% compare completed).  Use
code similar to the SEEK VB example with this code to find
duplicate files on a disk.

 Open "file1" For Binary As #1
 Open "file2" For Binary As #2
 issame% = True

 If LOF(1) <> LOF(2) Then
  issame% = False
  whole& = LOF(1) \ 10000         'number of whole 10,000 byte chunks
  part& = LOF(1) Mod 10000        'remaining bytes at end of file
  buffer1$ = String$(10000, 0)
  buffer2$ = String$(10000, 0)
  start& = 1
  For x& = 1 To whole&            'this for-next loop will get 10,000
    Get #1, start&, buffer1$      'byte chunks at a time.
    Get #2, start&, buffer2$
    If buffer1$ <> buffer2$ Then
     issame% = False
     Exit For
    End If
    start& = start& + 10000
  buffer1$ = String$(part&, 0)
  buffer2$ = String$(part&, 0)
  Get #1, start&, buffer1$        'get the remaining bytes at the end
  Get #2, start&, buffer2$        'get the remaining bytes at the end
  If buffer1$ <> buffer2$ Then issame% = False
 End If

 If issame% Then
  MsgBox "Files are identical", 64, "Info"
  MsgBox "Files are NOT identical", 16, "Info"
 End If

Return to Phil's Web Page

Created by: Philip