dcsimg
Results 1 to 7 of 7

Thread: [RESOLVED] Compare two textfiles

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2012
    Posts
    138

    Resolved [RESOLVED] Compare two textfiles

    Hello,

    I was wondering if there is an easier way to compare two textiles and display a message box to say there is a match/no match? the text files are not very big (only around 10 lines at most)

    Right now the only way i can do this is add a rich textbox on my form which displays all lines that match in both text files, i can then write this to a textfile and check the number of lines but I just feel this is a longer way of doing it rather than just checking if the text within both text files are identical

    This is what i have so far

    Code:
      Const TEST1 = "E:\File1.txt"
        Const TEST2 = "E:\File2.txt"
    
      Private Sub Button10_Click(sender As System.Object, e As System.EventArgs) Handles Button10.Click
            Dim file1 As New Dictionary(Of String, String)
            Dim file2 As New Dictionary(Of String, String)
    
            For Each line As String In System.IO.File.ReadAllLines(TEST1)
                Dim part() As String = line.Split(",")
                file1.Add(part(0), line)
    
            Next
    
            For Each line As String In System.IO.File.ReadAllLines(TEST2)
                Dim part() As String = line.Split(",")
                file2.Add(part(0), line)
            Next
    
            AddText("The following lines from " & TEST2 & " are also in " & TEST1)
    
            For Each key As String In file2.Keys
                If file1.ContainsKey(key) Then
                    AddText(file2(key))
                End If
            Next
    
        End Sub
    
        Private Sub AddText(ByVal text As String)
            txtResults.Text &= text & vbCrLf
            My.Computer.FileSystem.WriteAllText("E:\File3.txt", txtResults.Text, False)
        End Sub
    Any help is appreciated, thanks

  2. #2
    PowerPoster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,115

    Re: Compare two textfiles

    I got this off the internet somewhere...I wish I could give the proper credit. It writes the results to a listview.

    Code:
    Private Sub CompareVersions()
            Dim file As System.IO.StreamWriter
            Dim lv As ListViewItem
    
            Dim A() As String = IO.File.ReadAllLines("C:\SQL_Temp\DEV.txt")
            Dim B() As String = IO.File.ReadAllLines("C:\SQL_Temp\PROD.txt")
            Dim InA_notIn_B() As String = A.Except(B).ToArray
            Dim InB_notIn_A() As String = B.Except(A).ToArray
            If InA_notIn_B IsNot Nothing Then
                Dim MyStream As New StreamReader("C:\SQL_Temp\DEV.txt")
                Do While MyStream.Peek <> -1
    
                    lv = New ListViewItem
                    lv.Text = "InA_notInB - " & MyStream.ReadLine
                    ListView1.Items.Add(lv)
    
                Loop
            End If
            If InB_notIn_A IsNot Nothing Then
                IO.File.WriteAllLines("InB_notIn_A", InB_notIn_A)
            End If
    
        End Sub
    Please remember next time...elections matter!

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2012
    Posts
    138

    Re: Compare two textfiles

    Thanks, but its not what I'm trying to achieve as I dont want to use a listview or a rich textbox - I want to just check if the textfiles are identical and if they are (either yes/no) then i want to code further

    As far as mystream.peek and mystream.readlines they dont appear as options? not sure what version was used for that codings but i'm on vs 2010

  4. #4
    PowerPoster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,115

    Re: Compare two textfiles

    That was tested on VS 2013 Net framework 4. I didn't code anything specifically for you I just gave an example of a text comparison method. You are looping in your example so instead of .peek control the loop as you did above. Likewise the list view. Don't use it...just loop through and set a flag if you hit differences.
    Please remember next time...elections matter!

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,395

    Re: Compare two textfiles

    Since you're not worried about overhead, use IO.File.ReadAllText on both files and then compare the respective Strings. Here is an example where file1 and file2 are the same, but file3 is different:
    Code:
    'Read all of your files
    Dim file1 As String = IO.File.ReadAllText("file1.txt")
    Dim file2 As String = IO.File.ReadAllText("file2.txt")
    Dim file3 As String = IO.File.ReadAllText("file3.txt")
    
    'Compare if they're the same
    If file1 = file2 Then
      'They're the same
    Else
      'They're different
    End If
    
    If file2 = file3 Then			
      'They're the same
    Else
      'They're different
    End If
    Fiddle: Live Demo

  6. #6
    PowerPoster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,115

    Re: Compare two textfiles

    dday9's solution is great if you just want yes\no versus what is different.
    Please remember next time...elections matter!

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2012
    Posts
    138

    Re: Compare two textfiles

    Thanks guys, appreciate your efforts, i got what i needed with your help
    Code:
            'Read all of your files
            Dim file1 As String = IO.File.ReadAllText("E:\File1.txt")
            Dim file2 As String = IO.File.ReadAllText("E:\File2.txt")
    
            'Compare if they're the same
            If file1 = file2 Then
                MessageBox.Show("same")
                'They're the same
            Else
                MessageBox.Show("different")
                'They're different
            End If
    Last edited by VnewB; Feb 6th, 2018 at 09:55 AM. Reason: Added Code Tags

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width