Solved

Read last 3 line in text file

Posted on 2008-06-19
7
729 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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