Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. Imports System.IO
  2. Imports System.Threading
  3.  
  4. Module Module1
  5.  
  6. Sub Main()
  7.  
  8. Dim dirPath As String = My.Application.Info.DirectoryPath
  9.  
  10. Dim validAlfabeticFirstChars As Char() = "abcdefghijklmnopqrstuvwxyz"
  11. Dim charDict As SortedDictionary(Of String, List(Of String))
  12. Dim lineCount As Integer = 0
  13.  
  14. For Each f As FileInfo In New DirectoryInfo(dirPath).GetFiles("*.txt", SearchOption.TopDirectoryOnly)
  15.  
  16. charDict = New SortedDictionary(Of String, List(Of String))
  17. Debug.WriteLine(f.FullName)
  18.  
  19. Dim firstChar As String
  20.  
  21. For Each line As String In File.ReadLines(f.FullName)
  22. lineCount += 1
  23.  
  24. If line.Contains(":") Then
  25. line = line.Split(":").First()
  26. End If
  27. line = line.Replace(" "c, "").Trim({" "c, ControlChars.Tab, ControlChars.NullChar, ControlChars.Quote, ControlChars.Back}).ToLower()
  28.  
  29. If (String.IsNullOrWhiteSpace(line)) OrElse
  30. (Not line.Contains("@")) OrElse
  31. (line.Count(Function(c) c = "@"c) <> 1) Then
  32. Continue For
  33. End If
  34.  
  35. firstChar = line(0)
  36. Select Case firstChar
  37.  
  38. Case validAlfabeticFirstChars.ToArray()
  39. If Not charDict.ContainsKey(firstChar) Then
  40. charDict.Add(firstChar, New List(Of String))
  41. End If
  42. charDict(firstChar).Add(line)
  43.  
  44. Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
  45. If Not charDict.ContainsKey("0-9") Then
  46. charDict.Add("0-9", New List(Of String))
  47. End If
  48. charDict("0-9").Add(line)
  49. firstChar = "0-9"
  50.  
  51. Case Else
  52. Continue For
  53.  
  54. End Select
  55.  
  56. If (lineCount = 20000000) Then ' 20 Million
  57.  
  58. For Each kv As KeyValuePair(Of String, List(Of String)) In charDict
  59. kv.Value.Sort(StringComparer.Ordinal)
  60. kv.Value.Add(Environment.NewLine)
  61. File.AppendAllLines("C:\Mail Lists\" & If(kv.Key.Length = 1, Char.ToUpper(kv.Key), kv.Key) & ".txt", kv.Value)
  62. kv.Value.Clear()
  63. kv = Nothing
  64. Next
  65. charDict.Clear()
  66. GC.Collect()
  67. GC.WaitForFullGCComplete(Timeout.Infinite)
  68. lineCount = 0
  69. End If
  70.  
  71. Next
  72.  
  73. For Each kv As KeyValuePair(Of String, List(Of String)) In charDict
  74. kv.Value.Sort(StringComparer.Ordinal)
  75. kv.Value.Add(Environment.NewLine)
  76. File.AppendAllLines("C:\Mail Lists\" & If(kv.Key.Length = 1, Char.ToUpper(kv.Key), kv.Key) & ".txt", kv.Value)
  77. kv.Value.Clear()
  78. kv = Nothing
  79. Next
  80. charDict.Clear()
  81. charDict = Nothing
  82. GC.Collect()
  83. GC.WaitForFullGCComplete(Timeout.Infinite)
  84.  
  85. ' File.Delete(f.FullName)
  86. Next
  87.  
  88. MsgBox("Done !")
  89. End Sub
  90.  
  91. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement