CodeItBetter Programming Another VB Programming Blog

How to count number of lines in a text file

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
'Text File Handling - How to count number of lines in a text file
Public Function CountLineInTxtFile(ByVal strTxtFilePath As String) As Integer
    Dim strTxt As String
    Dim strLines() As String
 
    'Open the text file
    On Error GoTo CountLineInTxtFile_Error
 
    Open strTxtFilePath For Input As #1
    'load the text file in strTxt variable
    strTxt = Input(LOF(1), 1)
    'close the text file
    Close #1
 
    'store the lines
    strLines = Split(strTxt, vbCrLf)
 
    CountLineInTxtFile = UBound(strLines) - LBound(strLines)
 
    On Error GoTo 0
    Exit Function
 
CountLineInTxtFile_Error:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure " &_
        "CountLineInTxtFile of Module Module1"
End Function

Related posts:

  1. How to replace a line for particular line number
  2. How to Extract each and every word individually from a text file
  3. How to Search & Replace the Line for Given Search String in a text file
  4. Search & Delete the Line for Given Search String in a text file
  5. How to Remove all empty lines from the text file
  6. How to load a text file into list box for a given file name
  7. How to find last 120 lines in a text file
  8. How to I create a text file from part of another text file (for given line numbers like Line No. 2 to 7)
  9. How to Read Line By Line & Retrieve Each Word From Text File
  10. How to read text file contents for a given file (in four ways)

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.