How to read last two records in .txt file in VB?
Hi ,
I am very new to VB. Actually i am Mainframe resouce and i got an oppertunity to write hummingbird programming scrip which is equal to VB.
As i have tight deadlines, i am not able to spend much time on learning VB. Hence i am looking for quick help.
I got a situation to read the last two records in .TXT file for some comparision logic. So,
Can you please help on how to read the last two records in .TXT file?
How to read specific record in .TXT file?
how to write the record in beginning of the .txt file every time that we write (Append at first line)
Thanks a lot in advance for your in-time help. Look fwd for your valuable response. Thanks!
Regards
Agasthya
Re: How to read last two records in .txt file in VB?
Which version of VB are you using?
Re: How to read last two records in .txt file in VB?
Assuming VB6, you want something like this:-
vb Code:
'
Dim ff As Integer
Dim lines() As String
Dim fileName As String
Dim lineCount As Integer
ReDim lines(0)
'Replace "c:\text.txt" with your own file name
fileName = "c:\text.txt"
ff = FreeFile
Open fileName For Input As ff
Do Until EOF(ff)
lineCount = lineCount + 1
ReDim Preserve lines(0 To lineCount)
Line Input #ff, lines(lineCount)
Loop
Close ff
'Second to last line
MsgBox lines(lineCount - 1)
'Last line
MsgBox lines(lineCount)
That should also work in VBA. But if its VBScript you're using, I'm not certain that would work.
[EDIT]
Oh and on the off chance its VB.Net:-
vbnet Code:
'
'Replace "c:\text.txt" with your own file name
Dim fileName As String = "c:\text.txt"
Dim lines As String() = IO.File.ReadAllLines(filename)
'Second to last line
MessageBox.Show(lines(lines.Length - 2))
'Last line
MessageBox.Show(lines(lines.Length - 1))
Re: How to read last two records in .txt file in VB?
Hi Niya,
Thanks a lot for quick response.
It would be great if you can explain me the below two line what it does.. ?
ReDim Preserve lines(0 To lineCount)
Line Input #ff, lines(lineCount)
Thanks & Regards
Agasthyan.
Re: How to read last two records in .txt file in VB?
Quote:
Originally Posted by
Agasthyan
It would be great if you can explain me the below two line what it does.. ?
It would be even greater if you can tell us:
Quote:
Originally Posted by
Nightwalker83
Which version of VB are you using?
As you can see, Niya provided two examples written in two slightly different languages because he was unsure which VB you were actually talking about.
You said:
Quote:
Originally Posted by
Agasthyan
... i am looking for quick help.
Then you should help us understand your problem quickly. You can do so by supplying as much details about your problem as you can. Thank you.
Re: How to read last two records in .txt file in VB?
Hi All,
I am sorry for not providing the version of VB that i was talkning about. As i said in my first post, I am writing script in Hummingbird Programming Language which is mostly looks like VB basic.
I can say it's very basic VB version.
Thanks & Regards
Aga
Re: How to read last two records in .txt file in VB?
It seems the Hummingbird language is more closely related to VBScript rather than to VB6 (a.k.a. VB Classic). In that case, you should ask the moderators to move your thread to the VBScript forum. You may do so by clicking here.
I'm not familiar with the Hummingbird language but you may find this PDF guide helpful.
Re: How to read last two records in .txt file in VB?
Thread moved from the 'CodeBank VB6' forum (which is for you to post working code examples, not questions) to the 'VBScript' forum