Option Explicit Public Function get_file_line_count(fPath As String, Optional endline = "\r\n") As Long 'returns the file line count from a text file from a given file path 'the default line count is based on windows return carriage and newline characters. Use endline character "\n" to read unix lines '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 'if an error occurs -1 is returned Dim fso As Object, regex As Object, fData As String, lCount As Long Dim objfile As Object, objTS As Object 'read only mode when opening a text file Const READ_ONLY = 1 'Opens the file as ASCII. Const TristateFalse = 0 'error code return if the function fails to read Const PROCESS_ERROR = -1 On Error GoTo errhandler 'create regular expression object Set regex = CreateObject("vbScript.RegEXP") 'create file system object Set fso = CreateObject("Scripting.FileSystemObject") 'create file object Set objfile = fso.GetFile(fPath) 'create text stream object Set objTS = objfile.OpenAsTextStream(READ_ONLY, TristateFalse) 'Reads a specified number of characters from a TextStream file and returns the resulting string. fData = objTS.Read(objfile.Size) 'retrieve line count using regular expression pattern With regex .Global = True .Pattern = endline lCount = .Execute(fData).Count + 1 End With 'close stream object objTS.Close exitSuccess: 'return line count get_file_line_count = lCount 'release objects from memory Set fso = Nothing Set regex = Nothing Set objfile = Nothing Set objTS = Nothing Exit Function errhandler: Debug.Print Err.Number, Err.Description lCount = PROCESS_ERROR Resume exitSuccess End Function Sub sum_of_lines_in_files(sPath As String) 'display the total number of line counts txt files in a given directory Dim fso As Object Dim folder As Object Dim file As Object Dim i As Long Dim sum As Long Dim lines As Long On Error GoTo errhandler 'Create an instance of the FileSystemObject Set fso = CreateObject("Scripting.FileSystemObject") 'Get the folder object Set folder = fso.GetFolder(sPath) 'initilize to 0 sum = 0 'loops through each file in the directory and prints their names and path For Each file In folder.Files 'check if the file name ends with txt to get line count If file.Name Like "*.txt" Then lines = get_file_line_count(file.Path) sum = sum + lines End If Next file 'display the total of lines of all files combined Debug.Print "Total of lines in text files located " & sPath & " is " & sum exitSuccess: Set fso = Nothing Exit Sub errhandler: Debug.Print Err.Number, Err.Description Resume exitSuccess End Sub Sub count_file_lines_test() 'test reading file line count 'file location to read Const FILE_PATH = "C:\Users\codec\OneDrive\Documents\1000000-password-seclists.txt" 'display windows line counts ' Debug.Print "Windows line count section" Debug.Print "Line count with return carriage and newline terminator: " & get_file_line_count(FILE_PATH, "\n") ' Debug.Print "Line count without duplicate return carriage and newline terminator: " & get_file_line_count(FILE_PATH, "(\r\n)+") 'display unix line counts ' Debug.Print "Unix file count section" ' Debug.Print "Line count with newline terminator: " & get_file_line_count(FILE_PATH, "\n") ' Debug.Print "Line count without duplicate newline terminator: " & get_file_line_count(FILE_PATH, "(\n)+") ' sum_of_lines_in_files ("C:\Users\codec\OneDrive\Documents\") End Sub