472,337 Members | 1,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,337 software developers and data experts.

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

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
6 17331
"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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the...
3
by: Bruce | last post by:
Is there any mechanism in vs 2005 to have the IDE report the number of lines of source code in a C# project either per file or preferably across an...
14
by: mesterak | last post by:
I want to very quickly count the number of lines in text files without having to read each line and increment a counter. I am working in VB.NET and...
4
by: gualtmacchi | last post by:
I'm processing an XML input file getting a plain text file where from M nodes I got N output lines... It's not relevant but the input file is a...
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
3
by: javanooby | last post by:
Hi, I am having problems with this bit of code: public class main { public class readAccounts { reader1 r = new...
8
by: shivam001 | last post by:
I have the following file as the input APPLE 0 118 1 110 1 125 1 135 2 110 3 107 3 115 3 126 ORANGE 0 112 1 119 2 109 2 119 3 112 4 109 4 128 ...
13
by: humaid | last post by:
hi,guys i have done a program to count the number of bigrams. i have taken a input file by using @ARGV,then icounted the number of lines in the...
3
by: waynejr25 | last post by:
can anyone help me add a function that will count the occurance of each word in an input file. here's the code i have so far it counts the number of...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.