Compare Two Files in VB6

Visual Basic 6

Posted On 2012-11-25 by dwirch
Keywords:
Tags: Visual Basic 6 Scripting General  Windows
Views: 691

 

This function will allow you to compare one file to another. It will do either a lax check (compare file lengths only) or a stringent check (a byte by byte comparison).

Pretty straightforward demonstration of string handling for Visual Basic 6, posted by request.

Public Function AreTheyTheSame(ByVal File1 As String, _
ByVal File2 As String, Optional StringentCheck As _
Boolean = False) As Boolean

On Error GoTo ErrorHandler

If Dir(File1) = "" Then Exit Function
If Dir(File2) = "" Then Exit Function

Dim lLen1 As Long, lLen2 As Long
Dim iFileNum1 As Integer
Dim iFileNum2 As Integer
Dim bytArr1() As Byte, bytArr2() As Byte
Dim lCtr As Long, lStart As Long
Dim bAns As Boolean

lLen1 = FileLen(File1)
lLen2 = FileLen(File2)
If lLen1 <> lLen2 Then
Exit Function
ElseIf StringentCheck = False Then
AreTheyTheSame = True
Exit Function
Else
iFileNum1 = FreeFile
Open File1 For Binary Access Read As #iFileNum1
iFileNum2 = FreeFile
Open File2 For Binary Access Read As #iFileNum2

'put contents of both into byte Array
bytArr1() = InputB(LOF(iFileNum1), #iFileNum1)
bytArr2() = InputB(LOF(iFileNum2), #iFileNum2)
lLen1 = UBound(bytArr1)
lStart = LBound(bytArr1)

bAns = True
For lCtr = lStart To lLen1
If bytArr1(lCtr) <> bytArr2(lCtr) Then
bAns = False
Exit For
End If

Next
AreTheyTheSame = bAns

End If

ErrorHandler:
If iFileNum1 > 0 Then Close #iFileNum1
If iFileNum2 > 0 Then Close #iFileNum2
End Function

Comments On This Post

No comments on this post yet!

 

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.198.26.110

Before you can post, you need to prove you are human. If you log in, this test goes away.