|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Text Files: Number of LinesI'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. za***@construction-imaging.com wrote:
> I have never used the TextFieldParser method before, but I have used JFTR, it's more efficient to use select count(1) - you get the same result > 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. 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 <za***@construction-imaging.com> wrote in message Show quoteHide quote news:1140539911.413166.172170@g14g2000cwa.googlegroups.com... >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. > 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. "Chris Dunaway" <dunaw***@gmail.com> wrote in news:1140547600.629056.21350 @o13g2000cwo.googlegroups.com:> Does your progress bar need to reflect the number of lines? You could Sorry for the late reply... had no internet for 2 days.> 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. > > 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... Hi GanjaMan,
As you saw yourself, you don't know the number of lines before you have read them all. As Chris mentioned you can get the file size in bytes. Which is not always the size in char. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiofileinfoclasslengthtopic.asp However as Chris probably means can you estimate a char as 2 bytes therefore you can take the first line.lenght * 2 to measure a step. Just a thought, Cor Show quoteHide quote "TheGanjaMan" <theganjaman@somplace.invalid> schreef in bericht news:Xns97736DA46D382TheGanjaMan@207.46.248.16... > "Chris Dunaway" <dunaw***@gmail.com> wrote in news:1140547600.629056.21350 > @o13g2000cwo.googlegroups.com: > >> 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. Show quoteHide quote "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in news:OEmd$wFOGHA.2668@tk2msftngp13.phx.gbl: > > Hi GanjaMan, > > As you saw yourself, you don't know the number of lines before you > have read them all. > > As Chris mentioned you can get the file size in bytes. Which is not > always the size in char. > > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/ > html/frlrfsystemiofileinfoclasslengthtopic.asp > > However as Chris probably means can you estimate a char as 2 bytes > therefore you can take the first line.lenght * 2 to measure a step. > > Just a thought, > > Cor > > "TheGanjaMan" <theganjaman@somplace.invalid> schreef in bericht > news:Xns97736DA46D382TheGanjaMan@207.46.248.16... >> "Chris Dunaway" <dunaw***@gmail.com> wrote in >> news:1140547600.629056.21350 @o13g2000cwo.googlegroups.com: >> >>> 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.
Memory Leaking in VB.NET
switch form sqlserver to access at runtime Address parameter in WebClient.UploadFile? Items property How to change color line by line in a rich textbox? Q: Dataview and a grid Unable to open file remotely Which Database type to use? vb.net2005 got error when put object VB 2005 |
|||||||||||||||||||||||