Page tree
Skip to end of metadata
Go to start of metadata
  • The specific application was to do a file compare but it didn't matter where in the file a line existed to see if they were the same.

  • The code is below.

  • This code requires the "Microsoft Scripting Runtime" reference.

  • The code attempts to handle the case where there is more than one of the same line in a file. However, the line reported different will not be right if there are multiple lines that are the same

  • The code will also ignore blank lines.

     ' Code Module
    Sub Main
            Dim fn1 As String
            Dim fn2 As String
            Dim fn3 As String
    'current problem with this approach will be if there are duplicate lines.
            fn1 = Project.Path + "a.txt"
            fn2 = Project.Path + "b.txt"
            fn3 = Project.Path + "output.txt"
    
            Open fn3 For Output As #2
            Print #2, "Output from file compare utility"
            Print #2, vbTab & "File 1 is :" & fn1
            Print #2, vbTab & "File 2 is :" & fn2
            Print #2
    
            Dim kw As Dictionary
            Set kw = New Dictionary
    'collect up contents of file 1
            i = 0
            Open fn1 For Input As #1
            While Not EOF(1)
                    Line Input #1, L$
                    i = i+1
                    If Trim(L$) <> "" Then 'dont' look at white space
                            If kw.Exists(L$) Then 'if line exists, then update a count on the items, so items is line number,count
                                    tmp = kw(L$)
                                    tks = Split(tmp,",")
                                    n = CInt(tks(1))
                                    n = n + 1
                                    stg = tks(0) &"," & n
                                    kw(L$) = stg
                            Else
                                    stg = i &"," & 1
                                    kw.Add(L$,stg)
                            End If
                    End If
                    'Debug.Print L$
            Wend
            Close #1
    
            Print #2, "Lines found in file 2 that are not in file 1"
            i=0
            Open fn2 For Input As #1
            While Not EOF(1)
                    Line Input #1, L$
                    i=i+1
                    If Trim(L$) <> "" Then
                            If kw.Exists(L$)=False Then
                                    Print #2, vbTab & "Line " & i & ": " & L$
                            Else
                                    tmp = kw(L$)
                                    tks = Split(tmp,",")
                                    n = CInt(tks(1))
                                    n = n - 1
                                    stg = tks(0) &"," & n
                                    kw(L$) = stg
                            End If
                    End If
            '       Debug.Print L$
            Wend
            Close #1
            Print #2
    		Print #2, "Lines found in file 1 that are not in file 2"
            For i = 0 To kw.Count-1
                    tmp = kw.Items(i)
                    tks = Split(tmp,",")
                    n = CInt(tks(1))
                    If n <> 0 Then
                            Print #2, vbTab & "Line " & tks(0) & ": " & kw.Keys(i)
                    End If
            Next i
            Close #2
    End Sub