Advertisement
codecaine

Get line counts from files

Dec 2nd, 2018
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. Public Function get_file_line_count(fPath As String, Optional endline = "\r\n") As Long
  4. 'returns the file line count from a text file from a given file path
  5. 'the default line count is based on windows return carriage and newline characters. Use endline character "\n" to read unix lines
  6. 'if you want to count multiple lines that are together as one you can group the regex pattern like "(\r\n)+ or ("\n")+ for unix lines
  7. 'if an error occurs -1 is returned
  8.    Dim fso As Object, regex As Object, fData As String, lCount As Long
  9.     Dim objfile As Object, objTS As Object
  10.     'read only mode when opening a text file
  11.    Const READ_ONLY = 1
  12.     'Opens the file as ASCII.
  13.    Const TristateFalse = 0
  14.     'error code return if the function fails to read
  15.    Const PROCESS_ERROR = -1
  16.    
  17.     On Error GoTo errhandler
  18.     'create regular expression object
  19.    Set regex = CreateObject("vbScript.RegEXP")
  20.     'create file system object
  21.    Set fso = CreateObject("Scripting.FileSystemObject")
  22.     'create file object
  23.    Set objfile = fso.GetFile(fPath)
  24.     'create text stream object
  25.    Set objTS = objfile.OpenAsTextStream(READ_ONLY, TristateFalse)
  26.     'Reads a specified number of characters from a TextStream file and returns the resulting string.
  27.    fData = objTS.Read(objfile.Size)
  28.     'retrieve line count using regular expression pattern
  29.    With regex
  30.         .Global = True
  31.         .Pattern = endline
  32.         lCount = .Execute(fData).Count + 1
  33.     End With
  34.     'close stream object
  35.    objTS.Close
  36. exitSuccess:
  37.     'return line count
  38.    get_file_line_count = lCount
  39.     'release objects from memory
  40.    Set fso = Nothing
  41.     Set regex = Nothing
  42.     Set objfile = Nothing
  43.     Set objTS = Nothing
  44.     Exit Function
  45. errhandler:
  46.     Debug.Print Err.Number, Err.Description
  47.     lCount = PROCESS_ERROR
  48.     Resume exitSuccess
  49. End Function
  50.  
  51. Sub sum_of_lines_in_files(sPath As String)
  52. 'display the total number of line counts txt files in a given directory
  53.    Dim fso As Object
  54.     Dim folder As Object
  55.     Dim file As Object
  56.     Dim i As Long
  57.     Dim sum As Long
  58.     Dim lines As Long
  59.    
  60.     On Error GoTo errhandler
  61.     'Create an instance of the FileSystemObject
  62.    Set fso = CreateObject("Scripting.FileSystemObject")
  63.     'Get the folder object
  64.    Set folder = fso.GetFolder(sPath)
  65.  
  66.     'initilize to 0
  67.    sum = 0
  68.    
  69.     'loops through each file in the directory and prints their names and path
  70.    For Each file In folder.Files
  71.         'check if the file name ends with txt to get line count
  72.        If file.Name Like "*.txt" Then
  73.             lines = get_file_line_count(file.Path)
  74.             sum = sum + lines
  75.         End If
  76.     Next file
  77.     'display the total of lines of all files combined
  78.    Debug.Print "Total of lines in text files located " & sPath & " is " & sum
  79. exitSuccess:
  80.     Set fso = Nothing
  81.     Exit Sub
  82. errhandler:
  83.     Debug.Print Err.Number, Err.Description
  84.     Resume exitSuccess
  85. End Sub
  86.  
  87. Sub count_file_lines_test()
  88. 'test reading file line count
  89.    'file location to read
  90.    Const FILE_PATH = "C:\Users\codec\OneDrive\Documents\1000000-password-seclists.txt"
  91.     'display windows line counts
  92. '    Debug.Print "Windows line count section"
  93.    Debug.Print "Line count with return carriage and newline terminator: " & get_file_line_count(FILE_PATH, "\n")
  94. '    Debug.Print "Line count without duplicate return carriage and newline terminator: " & get_file_line_count(FILE_PATH, "(\r\n)+")
  95.    'display unix line counts
  96. '    Debug.Print "Unix file count section"
  97. '    Debug.Print "Line count with newline terminator: " & get_file_line_count(FILE_PATH, "\n")
  98. '    Debug.Print "Line count without duplicate newline terminator: " & get_file_line_count(FILE_PATH, "(\n)+")
  99. '    sum_of_lines_in_files ("C:\Users\codec\OneDrive\Documents\")
  100. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement