Solved

Read last 3 line in text file

Posted on 2008-06-19
7
728 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 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

18 Experts available now in Live!

Get 1:1 Help Now