Visual Basic File Duplicate Line Remover

Introduction: Hello, In this tutorial we will be making a simple duplicate line remover. This is made for text file lines but can be easily modified for many other purposes such as .csv duplicate value removers. Steps of Creation: Step 1: First we will create a new form and add: 1 - Button - To begin the duplicate line remover. 1 - Checkbox - To check if the program should overwrite the opened file Step 2: Now lets add a script to create OpenFileDialog to select the file to remove duplicate lines from:
  1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2. Dim fo As New OpenFileDialog
  3. fo.RestoreDirectory = True
  4. fo.Multiselect = False
  5. fo.Filter = "txt files (*.txt)|*.txt"
  6. fo.FilterIndex = 1
  7. fo.ShowDialog()
  8. If (Not fo.FileName = Nothing) Then
  9. End If
  10. End Sub
Step 3: Next let's create a new String List and add every line from the file to it. As we add them we will compare the line to the already added lines and if the line is not already added we will add it!
  1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2. Dim fo As New OpenFileDialog
  3. fo.RestoreDirectory = True
  4. fo.Multiselect = False
  5. fo.Filter = "txt files (*.txt)|*.txt"
  6. fo.FilterIndex = 1
  7. fo.ShowDialog()
  8. If (Not fo.FileName = Nothing) Then
  9. Dim lines As New List(Of String)
  10. Using sr As New System.IO.StreamReader(fo.FileName)
  11. While sr.Peek <> -1
  12. Dim line As String = sr.ReadLine()
  13. Dim isNew As Boolean = True
  14. For Each found As String In lines
  15. If (found = line) Then isNew = False
  16. Next
  17. If (isNew) Then lines.Add(sr.ReadLine())
  18. End While
  19. End Using
  20. End If
  21. End Sub
Step 3: Finally, let's create a SaveFileDialog if the Checkbox to overwite the file is not checked, otherwise we will write the values in the String List "lines" to the file we opened:
  1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2. Dim fo As New OpenFileDialog
  3. fo.RestoreDirectory = True
  4. fo.Multiselect = False
  5. fo.Filter = "txt files (*.txt)|*.txt"
  6. fo.FilterIndex = 1
  7. fo.ShowDialog()
  8. If (Not fo.FileName = Nothing) Then
  9. Dim lines As New List(Of String)
  10. Using sr As New System.IO.StreamReader(fo.FileName)
  11. While sr.Peek <> -1
  12. Dim line As String = sr.ReadLine()
  13. Dim isNew As Boolean = True
  14. For Each found As String In lines
  15. If (found = line) Then isNew = False
  16. Next
  17. If (isNew) Then lines.Add(sr.ReadLine())
  18. End While
  19. End Using
  20. Dim savePath As String
  21. If (CheckBox1.Checked) Then
  22. If (My.Computer.FileSystem.FileExists(fo.FileName)) Then
  23. My.Computer.FileSystem.DeleteFile(fo.FileName)
  24. End If
  25. savePath = fo.FileName
  26. Else
  27. Dim fs As New SaveFileDialog
  28. fs.RestoreDirectory = True
  29. fs.Filter = "txt files (*.txt)|*.txt"
  30. fs.FilterIndex = 1
  31. fs.ShowDialog()
  32. If (fs.FileName = Nothing) Then
  33. savePath = fo.FileName
  34. Else
  35. savePath = fs.FileName
  36. End If
  37. End If
  38. Using sw As New System.IO.StreamWriter(savePath)
  39. For Each line As String In lines
  40. sw.WriteLine(line)
  41. Next
  42. End Using
  43. End If
  44. End Sub
Project Completed! That's it! Below is the source code and you can download the project from the attached files below.
  1. Public Class Form1
  2. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  3. Dim fo As New OpenFileDialog
  4. fo.RestoreDirectory = True
  5. fo.Multiselect = False
  6. fo.Filter = "txt files (*.txt)|*.txt"
  7. fo.FilterIndex = 1
  8. fo.ShowDialog()
  9. If (Not fo.FileName = Nothing) Then
  10. Dim lines As New List(Of String)
  11. Using sr As New System.IO.StreamReader(fo.FileName)
  12. While sr.Peek <> -1
  13. Dim line As String = sr.ReadLine()
  14. Dim isNew As Boolean = True
  15. For Each found As String In lines
  16. If (found = line) Then isNew = False
  17. Next
  18. If (isNew) Then lines.Add(sr.ReadLine())
  19. End While
  20. End Using
  21. Dim savePath As String
  22. If (CheckBox1.Checked) Then
  23. If (My.Computer.FileSystem.FileExists(fo.FileName)) Then
  24. My.Computer.FileSystem.DeleteFile(fo.FileName)
  25. End If
  26. savePath = fo.FileName
  27. Else
  28. Dim fs As New SaveFileDialog
  29. fs.RestoreDirectory = True
  30. fs.Filter = "txt files (*.txt)|*.txt"
  31. fs.FilterIndex = 1
  32. fs.ShowDialog()
  33. If (fs.FileName = Nothing) Then
  34. savePath = fo.FileName
  35. Else
  36. savePath = fs.FileName
  37. End If
  38. End If
  39. Using sw As New System.IO.StreamWriter(savePath)
  40. For Each line As String In lines
  41. sw.WriteLine(line)
  42. Next
  43. End Using
  44. End If
  45. End Sub
  46. End Class

Add new comment