By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,095 Members | 950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,095 IT Pros & Developers. It's quick & easy.

HOW do I count the Number of lines in a text file

P: n/a
I'm using the streamreader stuff. I know how to write and append, but I
don't know how to get VB to return the number of lines that there are in the
sequential access file that I'm working with.

Example:
I've written 40 lines to the file. I know that, but how do I get the
computer to tell me how many lines I have writte to the file? something
like io.file.linecount would be nice, but I'm not finding anything like that
anywhere.

Thanks
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Daniel Kaseman" <da*@cfaith.com> schrieb:
I'm using the streamreader stuff. I know how to write and append, but I
don't know how to get VB to return the number of lines that there are in
the
sequential access file that I'm working with.


You'll either maintain a counter variable while writing lines to the file or
read the file line-by-line using a 'StreamReader' in order to determine the
number of lines.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2

P: n/a
yes, but what is the syntax?

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ev****************@TK2MSFTNGP14.phx.gbl...
"Daniel Kaseman" <da*@cfaith.com> schrieb:
I'm using the streamreader stuff. I know how to write and append, but I
don't know how to get VB to return the number of lines that there are in
the
sequential access file that I'm working with.
You'll either maintain a counter variable while writing lines to the file

or read the file line-by-line using a 'StreamReader' in order to determine the number of lines.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

P: n/a
"Daniel Kaseman" <da*@cfaith.com> schrieb:
yes, but what is the syntax?


Reading a text file line-by-line or blockwise with a progress indicator
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=readfile&lang=en>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #4

P: n/a
I'm using the streamreader stuff. I know how to write and append, but I
don't know how to get VB to return the number of lines that there are in
the
sequential access file that I'm working with.
You cannot write and append with a streamreader
Example:
I've written 40 lines to the file. I know that, but how do I get the
computer to tell me how many lines I have writte to the file?
Where is the file that you have readed, in memory?
something like io.file.linecount would be nice, but I'm not finding
anything like that
anywhere.

This I don't understand absolute not, what should it do, probably is the
file that you have readed in a kind of collection. You can get the
count/length from it.

Cor
Nov 21 '05 #5

P: n/a
In addition, if all the records are the same length, then you can get
the number of lines by dividing the file size by the record length.

Nov 21 '05 #6

P: n/a
There are two ways I would do this:

1)

Dim intLines As Integer = TextBox1.Lines.Length
MessageBox.Show(intLines, Me.Text)

2)

TextBox1.Text = ""
With OpenFileDialog1
.InitialDirectory =
Environment.GetFolderPath(Environment.SpecialFolde r.Desktop)
.Filter = "Text Files (*.txt)|*.txt"
If .ShowDialog <> DialogResult.OK Then Exit Sub
End With
Dim intLines As Integer = 0
Dim sr As New IO.StreamReader(OpenFileDialog1.FileName)
Do While sr.Peek() >= 0
TextBox1.Text += sr.ReadLine() & ControlChars.CrLf
intLines += 1
Loop
sr.Close()
MessageBox.Show(intLines, Me.Text)

I hope this helps

Crouchie1998
BA (HONS) MCP MCSE
Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.