Click to See Complete Forum and Search --> : Comparing two document files
kais
Aug 6th, 2001, 08:24 PM
How do i compare two doc files to check whether their are identical or not. If not i want the location where the difference is. IS there any API which can compare two files?
Thanks
Kais
Matthew Gates
Aug 6th, 2001, 09:26 PM
You could use the Like operator to tell if they are alike.
Just load them into two variables and compare them.
If Doc1 Like Doc2 Then Msgbox "Documents are the same"
Matthew Gates
Aug 6th, 2001, 09:40 PM
Note: If the documents are the same, and a word is in different caps, then the Like operator will pick the document up as being completely different. For example, "Hello" is not the same as "HELLO".
To prevent this and make "Hello" equal to "HELLO", add this line of code to the top of the Form Declarations.
Option Compare Text
Nucleus
Aug 7th, 2001, 05:28 PM
Try this function which should allow you to compare any two files including text files. It returns true if both files are exactly the same (binary comparison).
Function SameFile(ByVal Path1$, ByVal Path2$) As Boolean
'Nucleus
Dim ba1() As Byte, ba2() As Byte
Dim ff&, i&, u&
On Error Resume Next
If Len(Dir$(Path1)) And Len(Dir$(Path2)) Then
ff = FreeFile
Open Path1 For Binary As #ff
ReDim ba1(0 To LOF(ff) - 1)
Get #ff, , ba1
Close #ff
ff = FreeFile
Open Path2 For Binary As #ff
ReDim ba2(0 To LOF(ff) - 1)
Get #ff, , ba2
Close #ff
u = UBound(ba1)
If u = UBound(ba2) Then
For i = 0 To UBound(ba1)
If ba1(i) <> ba2(i) Then Exit For
Next
If i = u + 1 Then SameFile = True
End If
End If
End Function
Usage:
Private Sub Command1_Click()
MsgBox SameFile("C:\tmp\test.txt", "C:\tmp\test2.txt")
End Sub
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.