Results 1 to 2 of 2

Thread: How to read certain lines in a file and tell what is different

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    14

    How to read certain lines in a file and tell what is different

    Hello,
    I have 2 files that I am going to compare. I was wondering how would I go about to read those files, and tell what is different. Take for instance...that file1.txt has <name>Fred</Fred>
    <name>Barney</name>
    <name>This</name>
    File2.txt has...
    <name> Barn</name>
    <name> Fred</name>
    <name> This</name>

    Then I would have file box say file2.txt is different than file1.txt.
    Because it has more names than than file1.txt and also file2.txt is missing barney.

    Can someone tell me of a website that can help me with this, or can they help me themselves?

  2. #2
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: How to read certain lines in a file and tell what is different

    I have done this a while ago... it's a bit "clumsy", but it'll do - i'm sure it can be done easier, but hey

    VB Code:
    1. Dim arr1() As String, arr2() As String, arrDifferent() As String, line As String
    2. Dim i As Integer, j As Integer, n As Integer, k As Integer, _
    3.     counter As Integer, diffCounter As Integer, FF As Integer
    4.  
    5.     i = -1: FF = FreeFile
    6.        
    7.         'load first file
    8.         Open "c:\first.txt" For Input As #FF
    9.             Do Until EOF(FF)
    10.                 i = i + 1
    11.                     Line Input #FF, line
    12.                         line = LTrim(line): line = RTrim(line)
    13.                             If line <> vbNullString Then
    14.                                 ReDim Preserve arr1(i) As String
    15.                                     arr1(i) = line
    16.                             Else
    17.                                 i = i - 1
    18.                             End If
    19.             Loop
    20.         Close #FF
    21.  
    22.     i = -1: FF = FreeFile
    23.  
    24.         'load second file
    25.         Open "c:\second.txt" For Input As #FF
    26.             Do Until EOF(FF)
    27.                 i = i + 1
    28.                     Line Input #FF, line
    29.                         line = LTrim(line): line = RTrim(line)
    30.                             If line <> vbNullString Then
    31.                                 ReDim Preserve arr2(i) As String
    32.                                     arr2(i) = line
    33.                             Else
    34.                                 i = i - 1
    35.                             End If
    36.             Loop
    37.         Close #FF
    38.  
    39.     n = -1: diffCounter = 0
    40.  
    41.         'compare in one way
    42.         For i = 0 To UBound(arr1)
    43.             counter = 0
    44.                 For j = 0 To UBound(arr2)
    45.                     If arr1(i) = arr2(j) Then
    46.                         counter = counter + 1
    47.                             Exit For
    48.                     End If
    49.                 Next j
    50.                     If counter = 0 Then
    51.                         n = n + 1
    52.                             diffCounter = diffCounter + 1
    53.                                 ReDim Preserve arrDifferent(n) As String
    54.                                     arrDifferent(n) = arr1(i)
    55.                     End If
    56.         Next i
    57.  
    58.         'compare in second way
    59.         For i = 0 To UBound(arr2)
    60.             counter = 0
    61.                 For j = 0 To UBound(arr1)
    62.                     If arr2(i) = arr1(j) Then
    63.                         counter = counter + 1
    64.                             Exit For
    65.                     End If
    66.                 Next j
    67.                     If counter = 0 Then
    68.                         n = n + 1: counter = 0
    69.                             If diffCounter <> 0 Then
    70.                                 For k = 0 To UBound(arrDifferent)
    71.                                     If arrDifferent(k) = arr2(i) Then
    72.                                         counter = counter + 1
    73.                                             Exit For
    74.                                     End If
    75.                                 Next k
    76.                                     If counter = 0 Then
    77.                                         diffCounter = diffCounter + 1
    78.                                             ReDim Preserve arrDifferent(UBound(arrDifferent) + 1) As String
    79.                                                 arrDifferent(UBound(arrDifferent)) = arr2(i)
    80.                                     End If
    81.                             Else
    82.                                 diffCounter = diffCounter + 1
    83.                                     ReDim Preserve arrDifferent(0) As String
    84.                                         arrDifferent(UBound(arrDifferent)) = arr2(i)
    85.                             End If
    86.                     End If
    87.         Next i
    88.  
    89.             'display all the different ones... if there are any
    90.             If diffCounter <> 0 Then
    91.                 For i = 0 To UBound(arrDifferent)
    92.                     Debug.Print arrDifferent(i)
    93.                 Next i
    94.             Else
    95.                 MsgBox "There is no difference!"
    96.             End If

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