Code in VB6 showing text file differences:

Posted on 2004-11-23
Last Modified: 2010-05-02
Does anyone have some code I can put into my Application comparing 2 text files and showing the difference between the two files. If you have code for an entire app to do this that would be great, but if not thats OK too. I'm using VB6.
Question by:revis
    LVL 13

    Accepted Solution

    LVL 10

    Expert Comment

    try this

    Public Function AreTheyTheSame(ByVal File1 As String, _
      ByVal File2 As String, Optional StringentCheck As _
      Boolean = False) As Boolean
    'PURPOSE: Check to see if two files are identical
    'File1 and File2 = FullPaths of files to compare
    'StringentCheck (optional): If false (default),
    'will only compare file lengths.  If true, a
    'byte by byte comparison is conducted if file lengths are

    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
        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
        AreTheyTheSame = bAns
    End If
    If iFileNum1 > 0 Then Close #iFileNum1
    If iFileNum2 > 0 Then Close #iFileNum2
    End Function

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now