Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

[215] Count Lines in File

Last post 03-26-2005 8:02 AM by Pamenja. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 01-01-1999 12:00 AM

    [215] Count Lines in File

    This thread is for discussions of Count Lines in File.

    • Post Points: 0
  • 03-14-2003 5:19 PM In reply to

    in VB.net

    Here it is in VB.Net  with an error check to see if the file exists, and a check in case reading poops out somewhere mid stream:
    Code:

       Public Function CountLinesInFile(ByVal strFilePath As String) As Integer
           Dim lc As Integer = 0
           If Not File.Exists(strFilePath) Then
               MsgBox("File: " & strFilePath & _
                " hasn't been downloaded yet. Preprocessing is being aborted.",  _
               MsgBoxStyle.OKOnly, "File Does Not Exist")
           End If
           Try
               Dim sr As StreamReader = New StreamReader(strFilePath)
               While Not IsNothing(sr.ReadLine)
                   lc += 1
               End While
               sr.Close()
           Catch
               MsgBox("File: An error occurred while reading " & strFilePath & _
               ". Preprocessing is being aborted.", MsgBoxStyle.OKOnly, "File Read Error")
               lc = -1
           End Try
           Return lc
       End Function
    • Post Points: 0
  • 03-26-2005 8:02 AM In reply to

    • Pamenja
    • Not Ranked
    • Joined on 03-26-2005
    • New Member
    • Points 5

    Countlines counts too many line

    I used the code provided by James to count the number of lines in a textfile. Looking at my text file in an advanced "notepad" editor it had 3533 lines. The CountLines procedure counted 5043 lines.

    So I looked at the CountLines procedure and put Line in front of Input #fileFile, strBuffer (inside the loop)

    Now I get the right number of lines. I don't really know why I got more lines using the original code from James, but now I can use the total number of lines to get my progressbar (John Walkenbach's  progressbar actually) to work.
    • Post Points: 0
Page 1 of 1 (3 items)