Recipe 12.23. Comparing Two Files for Equality


You have two files that should contain identical content, and you want to make sure that they do.


Sample code folder: Chapter 12\CompareFiles

Call the GenerateFileChecksum() routine developed in Recipe 12.22 for each of the files, and compare the checksum.


The following code uses the GenerateFileChecksum() method on two distinct files and compares the resulting checksums:

 Public Function AreFilesIdentical( _       ByVal file1 As String, ByVal file2 As String) _       As Boolean    ' ----- Return True if two files are identical.    Dim checksum1 As Byte( )    Dim checksum2 As Byte( )    Dim counter As Integer    On Error GoTo ErrorHandler    ' ------ Calculate the checksums.    checksum1 = GenerateFileChecksum(file1)    checksum2 = GenerateFileChecksum(file2)    ' ----- See if the results are equal.    For counter = 0 To UBound(checksum1)       If (checksum1(counter) <> checksum2(counter)) _          Then Return False    Next counter    ' ----- The checksums are equal.    Return True ErrorHandler:    ' ----- If anything went wrong, assume the    '       files are unequal.    Return False End Function 

See Also

See Recipe 12.22 for the code needed to complete this recipe.

Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400 © 2008-2017.
If you may any questions please contact us: