File Comparison Tool in Visual Basic
Submitted by Yorkiebar on Wednesday, April 23, 2014 - 10:18.
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...
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)...
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...
Counting Lines:
To count the amount of lines we simply count the string lists...
Drawing the Stats:
Finally we just add the stats to the listboxes. We add the file names, paths, words and lines...
- Dim path1 As String = Nothing
- Dim path2 As String = Nothing
- Using fo As New OpenFileDialog
- fo.Filter = "Text Files |*.txt"
- fo.FilterIndex = 1
- fo.Multiselect = False
- fo.RestoreDirectory = True
- fo.ShowDialog()
- path1 = fo.FileName
- fo.ShowDialog()
- path2 = fo.FileName
- End Using
- Dim lines1 As New List(Of String)
- Dim lines2 As New List(Of String)
- Using sr As New System.IO.StreamReader(path1)
- While (sr.Peek <> -1)
- lines1.Add(sr.ReadLine())
- End While
- End Using
- Using sr As New System.IO.StreamReader(path2)
- While (sr.Peek <> -1)
- lines2.Add(sr.ReadLine())
- End While
- End Using
- Dim words1 As Integer = 0
- Dim words2 As Integer = 0
- For Each line As String In lines1
- Dim words As String() = line.Split(" ")
- words1 += words.Count
- Next
- For Each line As String In lines2
- Dim words As String() = line.Split(" ")
- words2 += words.Count
- Next
- lines1.Count.ToString()
- lines2.Count.ToString())
- Public Class Form1
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim path1 As String = Nothing
- Dim path2 As String = Nothing
- Using fo As New OpenFileDialog
- fo.Filter = "Text Files |*.txt"
- fo.FilterIndex = 1
- fo.Multiselect = False
- fo.RestoreDirectory = True
- fo.ShowDialog()
- path1 = fo.FileName
- fo.ShowDialog()
- path2 = fo.FileName
- End Using
- If (path1 = Nothing Or path2 = Nothing) Then
- MsgBox("One of the selected files is not correct. Please try again.")
- Else
- Dim lines1 As New List(Of String)
- Dim lines2 As New List(Of String)
- Using sr As New System.IO.StreamReader(path1)
- While (sr.Peek <> -1)
- lines1.Add(sr.ReadLine())
- End While
- End Using
- Using sr As New System.IO.StreamReader(path2)
- While (sr.Peek <> -1)
- lines2.Add(sr.ReadLine())
- End While
- End Using
- Dim words1 As Integer = 0
- Dim words2 As Integer = 0
- For Each line As String In lines1
- Dim words As String() = line.Split(" ")
- words1 += words.Count
- Next
- For Each line As String In lines2
- Dim words As String() = line.Split(" ")
- words2 += words.Count
- Next
- ListBox1.Items.Add(path1.Split("\")(path1.Split("\").Count() - 1))
- ListBox1.Items.Add(path1)
- ListBox1.Items.Add(words1.ToString())
- ListBox1.Items.Add(lines1.Count.ToString())
- ListBox2.Items.Add(path2.Split("\")(path2.Split("\").Count() - 1))
- ListBox2.Items.Add(path2)
- ListBox2.Items.Add(words2.ToString())
- ListBox2.Items.Add(lines2.Count.ToString())
- End If
- End Sub
- End Class
Add new comment
- 65 views