Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. Imports System.IO
  2. Imports System.Text
  3.  
  4. Module Module1
  5.  
  6. ' Removes duplicated lines from any text file that is found inside the working directory.
  7. ' It generates a backup file.
  8.  
  9. Sub Main()
  10.  
  11. Dim dirPath As String = My.Application.Info.DirectoryPath
  12. Dim curLines As Integer
  13. Dim totalLines As Integer
  14. Dim totalFiles As Integer
  15. Dim lineSet As New HashSet(Of String)(StringComparer.Ordinal)
  16. Dim enc As Encoding = Encoding.Default
  17. Dim sw As New Stopwatch()
  18.  
  19. For Each fi As FileInfo In New DirectoryInfo(dirPath).GetFiles("*.txt", SearchOption.TopDirectoryOnly)
  20.  
  21. totalFiles += 1
  22.  
  23. Console.WriteLine(String.Format("Parsing file....: {0}", fi.FullName))
  24. sw.Restart()
  25. For Each line As String In File.ReadLines(fi.FullName, enc)
  26. curLines += 1
  27. lineSet.Add(line)
  28. Next line
  29. sw.Stop()
  30.  
  31. Console.WriteLine(String.Format("Total lines.....: {0}", curLines.ToString("n0")))
  32. Console.WriteLine(String.Format("Acchieved lines.: {0}", lineSet.Count.ToString("n0")))
  33. Console.WriteLine(String.Format("Duplicated lines: {0}", (curLines - lineSet.Count).ToString("n0")))
  34. Console.WriteLine(String.Format("Time elapsed....: {0}", sw.Elapsed.ToString("mm\:ss\:ffff")))
  35. Console.WriteLine(String.Format("Saving file.....: {0}", fi.FullName))
  36.  
  37. Dim bakFilePath As String = Path.Combine(fi.DirectoryName, fi.Name & ".bak")
  38. fi.CopyTo(bakFilePath, overwrite:=True)
  39. File.WriteAllLines(fi.FullName, lineSet, enc)
  40.  
  41. lineSet.Clear()
  42. totalLines += curLines
  43. curLines = 0
  44.  
  45. Console.WriteLine(Environment.NewLine)
  46. Next
  47.  
  48. Console.WriteLine(String.Format("Total amount of text files parsed..: {0}", totalFiles))
  49. Console.WriteLine(String.Format("Total amount of lines in text files: {0}", totalLines))
  50.  
  51. Console.WriteLine("Press any key to exit...")
  52. Console.ReadKey(intercept:=True)
  53. Environment.Exit(0)
  54.  
  55. End Sub
  56.  
  57. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement