Solved

Read last 3 line in text file

Posted on 2008-06-19
7
732 Views
Last Modified: 2008-06-20
I want to read the last 3 line in a text file. I can open and read it line by line and all, but not sure how to read the last 3 line, VB.net please. Thank you
0
Comment
Question by:VBdotnet2005
  • 3
  • 3
7 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 21826936
Approximately how many line are in the file?
...and what version VB?
0
 

Expert Comment

by:dstwins
ID: 21827549
Start by assigning each line into an Array  .. arrays by default are numbered.

assign it to a variable.

a = a + 1
readline(0)  =  Welcome
readline(1)  =  to
readline(2)  =  Mars
readline(3)  =  Earthling
a = 3
-
messagebox.show( readline(a-2))
messagebox.show( readline(a-1))
messagebox.show( readline(a))


There's an example...


0
 

Author Comment

by:VBdotnet2005
ID: 21827717
Approximately how many line are in the file?  > many  - this a VB.net process that keep append to this file
...and what version VB?                                  > VB9
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 21827751
You could try something like this:
        Dim lines() As String
        lines = My.Computer.FileSystem.ReadAllText("c:\someFile.txt").Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
        If lines.Length >= 2 Then
            Debug.Print("3rd to Last: " & lines(lines.Length - 3))
            Debug.Print("2nd to Last: " & lines(lines.Length - 2))
            Debug.Print("       Last: " & lines(lines.Length - 1))
        End If

Open in new window

0
 

Author Comment

by:VBdotnet2005
ID: 21828610
Idle_Mind,
Could you explain a little bit ? This is the first time I have seen "My.Computer..."

lines = My.Computer.FileSystem.ReadAllText("c:\someFile.txt").Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
0
 

Author Comment

by:VBdotnet2005
ID: 21828627
         
All gives me line number only... ?

            Debug.Print("3rd to Last: " & lines(lines.Length - 3))
            Debug.Print("2nd to Last: " & lines(lines.Length - 2))
            Debug.Print("       Last: " & lines(lines.Length - 1))
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 21828671
The "My.XXX.YYY" functions were introduced in VB.Net 2005.  They are "convenience" functions which make it easy to accomplish many common tasks.
See "Development with My": http://msdn.microsoft.com/en-us/library/5btzf5yk.aspx
ReadAllText() returns the ENTIRE file as ONE String.
I then call Split() on it, using vbCrLF as the delimiter.
    (The RemoveEmptyEntries flag gets rid of the blank lines caused by changing vbCrLf to a Character Array.)
The result is an Array of String in "lines" with each element representing each line in the file.
So index 0 (zero) is the first line.
Index 1 is the second line.
Index 2 is the third line. etc...
To get the last three lines we using ".Length - X" where X = 3,2,1
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question