Solved

Read last 3 line in text file

Posted on 2008-06-19
7
727 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now