Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Read last 3 line in text file

Posted on 2008-06-19
Medium Priority
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, please. Thank you
Question by:VBdotnet2005
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 86

Expert Comment

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

Expert Comment

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
- readline(a-2)) readline(a-1)) readline(a))

There's an example...


Author Comment

ID: 21827717
Approximately how many line are in the file?  > many  - this a process that keep append to this file
...and what version VB?                                  > VB9
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 86

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


Author Comment

ID: 21828610
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)

Author Comment

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))
LVL 86

Accepted Solution

Mike Tomlinson earned 500 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":
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

Featured Post

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.

Question has a verified solution.

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

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

721 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