Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read specific line from bottom of text file 1

Status
Not open for further replies.

mirror152

Technical User
Aug 17, 2007
9
US
Hi,

I have text files that store data. Once open (no problem there) I need to read the fourth line from the bottom for use in my program. I'm using text files of varying length so going from the top won't work. Ideas?
 
I suggest you use arrays for this. Store each line in an element of an array. Then you can simply use uBound -3. Like this:

Code:
    Dim Data() As String
    
    Data = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile("[!]C:\Lines.txt[/!]").ReadAll, vbCrLf)
    Call MsgBox(Data(UBound(Data) - 3))

Make sure you replace the filename without whatever file you are using.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Alternatively (a slighly more .Net-ish way) ...

Code:
		Try
			Dim lines() As String = IO.File.ReadAllLines(My.Computer.FileSystem.SpecialDirectories.Desktop + "\lines.txt")
			MessageBox.Show(lines(lines.Length - 4))
		Catch ex As Exception
			MessageBox.Show(ex.Message)
		End Try
 
I got a little confused. I thought this was the VB6 forum. I see now that I was mistaken. Sorry.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top