Results 1 to 4 of 4

Thread: Comparing two document files

  1. #1
    kais
    Guest

    Comparing two document files

    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

  2. #2
    Matthew Gates
    Guest
    You could use the Like operator to tell if they are alike.
    Just load them into two variables and compare them.


    VB Code:
    1. If Doc1 Like Doc2 Then Msgbox "Documents are the same"

  3. #3
    Matthew Gates
    Guest
    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:
    1. Option Compare [color=#00007F]Text[/color]

  4. #4
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    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:
    1. Function SameFile(ByVal Path1$, ByVal Path2$) As Boolean
    2. 'Nucleus
    3.  Dim ba1() As Byte, ba2() As Byte
    4.  Dim ff&, i&, u&
    5.  
    6.  On Error Resume Next
    7.  If Len(Dir$(Path1)) And Len(Dir$(Path2)) Then
    8.     ff = FreeFile
    9.     Open Path1 For Binary As #ff
    10.     ReDim ba1(0 To LOF(ff) - 1)
    11.     Get #ff, , ba1
    12.     Close #ff
    13.  
    14.     ff = FreeFile
    15.     Open Path2 For Binary As #ff
    16.     ReDim ba2(0 To LOF(ff) - 1)
    17.     Get #ff, , ba2
    18.     Close #ff
    19.  
    20.     u = UBound(ba1)
    21.     If u = UBound(ba2) Then
    22.         For i = 0 To UBound(ba1)
    23.             If ba1(i) <> ba2(i) Then Exit For
    24.         Next
    25.         If i = u + 1 Then SameFile = True
    26.     End If
    27.  End If
    28. End Function

    Usage:
    VB Code:
    1. Private Sub Command1_Click()
    2. MsgBox SameFile("C:\tmp\test.txt", "C:\tmp\test2.txt")
    3. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width