Results 1 to 6 of 6
  1. #1
    2 Star Lounger
    Join Date
    Oct 2008
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Last line number in a text file (2002/SP3)

    Is there a way to get the total number of lines in a text file with out reading the whole file line by line, ie, what's the last line number?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Last line number in a text file (2002/SP3)

    You could open the file in Word, and get ActiveDocument.Paragraphs.Count, or open the file in Excel, and retrieve ActiveSheet.UsedRange.Rows.Count.

  3. #3
    2 Star Lounger
    Join Date
    Oct 2008
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Last line number in a text file (2002/SP3)

    Thanks Hans. But is there a way to do this without opening the text file directly in Word or Excel. Is there a shell command that looks at the file and gives you the number of lines/rows?

  4. #4
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Last line number in a text file (2002/SP3)

    You could try this piece of code in an excel workbook assuming the file is in a fixed location

    Sub CountTextFile()


    Dim filenum As Integer
    Dim count As Long
    Dim tmp As String
    filenum = FreeFile
    Open "c:test.txt" For Input As filenum
    Do While Not EOF(filenum)
    Line Input #filenum, tmp$
    count = count + 1
    Loop
    Close #filenum

    MsgBox count
    End Sub
    Jerry

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Last line number in a text file (2002/SP3)

    Not as far as I know. Text files are just that - they don't contain any metadata.

  6. #6
    2 Star Lounger
    Join Date
    Oct 2008
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Last line number in a text file (2002/SP3)

    Good info. Thank you Jerry and Hans.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •