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
Printable View
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
You could use the Like operator to tell if they are alike.
Just load them into two variables and compare them.
VB Code:
If Doc1 Like Doc2 Then Msgbox "Documents are the same"
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.
VB Code:
Option Compare [color=#00007F]Text[/color]
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).
VB Code:
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:
VB Code:
Private Sub Command1_Click() MsgBox SameFile("C:\tmp\test.txt", "C:\tmp\test2.txt") End Sub