File Comparison Tool in Visual Basic

Introduction: This tutorial is on how to create a file comparison tool in Visual Basic. Pseudo: First the user selects file #1. Then file #2. Stats are accumulated for each file. Stats are drawn on to the form. Design: For the form, we are simply going to use two listboxes (listbox1 and listbox2) to hold the stats and 'button1' with the text of 'Begin' to start the comparison process. File Selection: So now we create an OpenFileDialog to allow the user to select a file. We can re-use the same dialog twice by saving the path to a variable and then allowing the user to re-select the file path through the same dialog (by relaunching it). We set the filter to .txt (text files) only, and multi-select is disabled since we do not want more than two files...
  1. Dim path1 As String = Nothing
  2. Dim path2 As String = Nothing
  3. Using fo As New OpenFileDialog
  4.         fo.Filter = "Text Files |*.txt"
  5.         fo.FilterIndex = 1
  6.         fo.Multiselect = False
  7.         fo.RestoreDirectory = True
  8.         fo.ShowDialog()
  9.         path1 = fo.FileName
  10.         fo.ShowDialog()
  11.         path2 = fo.FileName
  12. End Using
Reading the Files: Next we want to create two string lists and read each line of each file to the the appropriate list (lines1 = file1, lines2 = file2)...
  1. Dim lines1 As New List(Of String)
  2. Dim lines2 As New List(Of String)
  3. Using sr As New System.IO.StreamReader(path1)
  4.         While (sr.Peek <> -1)
  5.                 lines1.Add(sr.ReadLine())
  6.         End While
  7. End Using
  8. Using sr As New System.IO.StreamReader(path2)
  9.         While (sr.Peek <> -1)
  10.                 lines2.Add(sr.ReadLine())
  11.         End While
  12. End Using
Counting Words: As the first stat, we are going to count the words of each file. We simply create a variable to hold the amount of words, set it to zero and iterate through each line. One each iteration we split the line by a space to get the words, and then add the amount of words to the total count. Finally we do the same for file2...
  1. Dim words1 As Integer = 0
  2.         Dim words2 As Integer = 0
  3.         For Each line As String In lines1
  4.                 Dim words As String() = line.Split(" ")
  5.                 words1 += words.Count
  6.         Next
  7.         For Each line As String In lines2
  8.                 Dim words As String() = line.Split(" ")
  9.         words2 += words.Count
  10. Next
Counting Lines: To count the amount of lines we simply count the string lists...
  1. lines1.Count.ToString()
  2. lines2.Count.ToString())
Drawing the Stats: Finally we just add the stats to the listboxes. We add the file names, paths, words and lines...
  1. Public Class Form1
  2.  
  3.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  4.         Dim path1 As String = Nothing
  5.         Dim path2 As String = Nothing
  6.         Using fo As New OpenFileDialog
  7.             fo.Filter = "Text Files |*.txt"
  8.             fo.FilterIndex = 1
  9.             fo.Multiselect = False
  10.             fo.RestoreDirectory = True
  11.             fo.ShowDialog()
  12.             path1 = fo.FileName
  13.             fo.ShowDialog()
  14.             path2 = fo.FileName
  15.         End Using
  16.  
  17.         If (path1 = Nothing Or path2 = Nothing) Then
  18.             MsgBox("One of the selected files is not correct. Please try again.")
  19.         Else
  20.             Dim lines1 As New List(Of String)
  21.             Dim lines2 As New List(Of String)
  22.             Using sr As New System.IO.StreamReader(path1)
  23.                 While (sr.Peek <> -1)
  24.                     lines1.Add(sr.ReadLine())
  25.                 End While
  26.             End Using
  27.             Using sr As New System.IO.StreamReader(path2)
  28.                 While (sr.Peek <> -1)
  29.                     lines2.Add(sr.ReadLine())
  30.                 End While
  31.             End Using
  32.  
  33.             Dim words1 As Integer = 0
  34.             Dim words2 As Integer = 0
  35.             For Each line As String In lines1
  36.                 Dim words As String() = line.Split(" ")
  37.                 words1 += words.Count
  38.             Next
  39.             For Each line As String In lines2
  40.                 Dim words As String() = line.Split(" ")
  41.                 words2 += words.Count
  42.             Next
  43.            
  44.             ListBox1.Items.Add(path1.Split("\")(path1.Split("\").Count() - 1))
  45.             ListBox1.Items.Add(path1)
  46.             ListBox1.Items.Add(words1.ToString())
  47.             ListBox1.Items.Add(lines1.Count.ToString())
  48.             ListBox2.Items.Add(path2.Split("\")(path2.Split("\").Count() - 1))
  49.             ListBox2.Items.Add(path2)
  50.             ListBox2.Items.Add(words2.ToString())
  51.             ListBox2.Items.Add(lines2.Count.ToString())
  52.         End If
  53.     End Sub
  54. End Class

Add new comment