Results 1 to 9 of 9

Thread: Compare Text Files

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 1999
    Posts
    153

    Compare Text Files

    I'm trying to compare two text files and have the difference displayed.
    The code below displays both text files, just haven't figured out how to get what's not in either file.

    FileOpen(1, File1.Value, OpenMode.Input, OpenAccess.Read, OpenShare.Shared)
    FileOpen(2, File2.Value, OpenMode.Input, OpenAccess.Read, OpenShare.Shared)
    Do While Not EOF(1)
    Dim TextLine As String = LineInput(1)
    Response.Write(TextLine)
    Response.Write("<br>")
    Loop
    FileClose(1)

    Do While Not EOF(2)
    Dim TextLine2 As String = LineInput(2)
    Response.Write(Textline2)
    Response.Write("<br>")
    Loop
    FileClose(2)

    Or if you have a better way of comparing two text files I'm open. I'm using vb.net and running a web form.
    Thanks
    Last edited by Hutty; Sep 20th, 2005 at 11:02 AM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Compare Text Files

    Read each file, line by line and store it in an array. Then loop through the arrays and simply check whether each element of the array (by ordinal) matches the other one. If they match, do nothing, if they don't match, mark them.

    Finally, when displaying the two test files, make sure you 'highlight' the differing lines in your display.

  3. #3
    Fanatic Member -TPM-'s Avatar
    Join Date
    Jul 2005
    Posts
    850

    Re: Compare Text Files

    For a straight compare the easiest way would be to hash the files and then compare the hashes.
    TPM

    Add yourself to the VBForums Frappr Map!!

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 1999
    Posts
    153

    Re: Compare Text Files

    Thanks for responding. I've managed to get the code where it's reading and writing data to the console. Would ultimately want it in a listbox or datagrid though. But anyway, when the lines are displayed it's multiple lines on one line.

    I'm getting below on one line. A separate line should begin with CO574.

    CO574.GL,11100,21 CO574.GL,12110.01,3970 CO574.GL,12110.02,1104 CO574.GL,12110.02,4

    Here's the code that produce the results above.

    For Each strFile1Line As String In strFile1Lines
    Dim bLineInFile2 As Boolean = False

    ' Compare the current line in File1 against all lines in File2
    For Each strFile2Line As String In strFile2Lines
    If strFile1Line = strFile2Line Then bLineInFile2 = True

    Next

    ' If the line is in not in File2 add it to the variable strFile1NotFile2
    If Not bLineInFile2 Then strFile1NotFile2 = strFile1NotFile2 & vbCrLf & strFile1Line & vbCrLf

    Next
    Response.Write(strFile1NotFile2 & vbCrLf)
    Response.Write("<br>")

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 1999
    Posts
    153

    Re: Compare Text Files

    I figured it out and it's working the way I want it to on small files. When I try to read a large files it returns "page not found". These files are about 5k, but my ultimate goal is to read files much larger, maybe 30-50mb. Any ideas how to get around current problem?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 1999
    Posts
    153

    Re: Compare Text Files

    Just a followup to see if there are any ideas. Thanks.

  7. #7
    New Member
    Join Date
    Jan 2008
    Posts
    1

    Re: Compare Text Files


  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Compare Text Files

    It's been 2 years...

  9. #9
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: Compare Text Files

    BeyondCompare 2. Wonderful software. Even connects over networks and FTP.

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