Text Files: Number of Lines

  • Thread starter Thread starter TheGanjaMan
  • Start date Start date
T

TheGanjaMan

Hi people,

I'm stuck on a problem and I was wondering if there was a way around:
I'm trying to find the number of lines in a comma delimited text file.

I have a progress bar that should display the progress of a text file that
is being read into a DataGridView. I would like the progressbar to
progress as the lines of data are being read.

But to set the maximum value for the progress bar I would need to know the
maximum line number of the text file.

I've used the following code to open the file, and
Dim myReader As New Microsoft.VisualBasic.FileIO.TextFieldParser
("text.csv", System.Text.Encoding.Default)
myReader.TextFieldType = FileIO.FieldType.Delimited
myReader.SetDelimiters(";")

I can read the contents of the file with a do loop, but is there a way to
get the number of lines in the file without a do loop statement? if not
I'd have to do a do loop to count the lines and set the number of lines to
the progressbar.maximum, and then do loop again to read the file with the
progressbar.value set to the current line in the loop.

Any ideas...
Thanks.
 
I have never used the TextFieldParser method before, but I have used
the Microsoft Text driver to read CSV files with a standard SQL Select
style statement. And I obtained the record count by running a Select
count(*) query on the file.
 
I have never used the TextFieldParser method before, but I have used
the Microsoft Text driver to read CSV files with a standard SQL Select
style statement. And I obtained the record count by running a Select
count(*) query on the file.

JFTR, it's more efficient to use select count(1) - you get the same result
but it doesn't have to bother with the everything represented by *.

Andrew
 
What if you were to be sneaky about it?

Sub ReadMe()

Dim sFile As String = "c:\logs\bigfile"
Dim iHasRead As Decimal, iToRead As Decimal
Dim fi As System.IO.FileInfo

fi = New System.IO.FileInfo(sFile)
iToRead = fi.Length

Using MyReader As New
Microsoft.VisualBasic.FileIO.TextFieldParser(sfile)

MyReader.TextFieldType =
Microsoft.VisualBasic.FileIO.FieldType.Delimited
MyReader.Delimiters = New String() {","}

Dim currentRow As String()
'Loop through all of the fields in the file.
'If any lines are corrupt, report an error and continue parsing.

While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
' Include code here to handle the row.

' I don't know how to do a lenb(obj) in .Net yet.
' So we just loop over our items.
For Each s As String In currentRow
iHasRead += s.Length
'Process s
Next

RaiseEvent Progress(iToRead, iHasRead)

Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & _
" is invalid. Skipping")

End Try

End While

End Using

End Sub
 
Does your progress bar need to reflect the number of lines? You could
base the progress bar on the file size. Set the Maximum value of the
progress to the file size and every time you read a line, advance the
progress bar by the number of bytes in the line you read.
 
Does your progress bar need to reflect the number of lines? You could
base the progress bar on the file size. Set the Maximum value of the
progress to the file size and every time you read a line, advance the
progress bar by the number of bytes in the line you read.

Sorry for the late reply... had no internet for 2 days.

well the progress bar doesn't have to reflect the number of lines,
I thought it would be easier to update the progressbar.value within the
do while statement when each line was read.
If there is an easier way please tell me...

I'm open to suggestions. But the fact is I haven't been able to count the
lines in the file without running through it at least once. So I wouldn't
want to count the lines and run through the file again just to get a
progressbar working, but if thats the only way... then be it...
 
Thank you all for your input.
I think reading the filesize with the fileio and updating the value
within the loop seems to be the best solution for now, without having to
go through the file twice.

thank you all for your help.
 
Back
Top