|
-
May 22nd, 2013, 03:52 PM
#1
Thread Starter
New Member
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
-
May 22nd, 2013, 10:11 PM
#2
Re: How to read last two records in .txt file in VB?
Which version of VB are you using?
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
May 22nd, 2013, 10:27 PM
#3
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))
Last edited by Niya; May 22nd, 2013 at 10:34 PM.
-
May 23rd, 2013, 09:24 AM
#4
Thread Starter
New Member
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.
-
May 23rd, 2013, 11:07 AM
#5
Re: How to read last two records in .txt file in VB?
 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:
 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:
 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.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
May 24th, 2013, 10:50 AM
#6
Thread Starter
New Member
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
-
May 24th, 2013, 11:33 AM
#7
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.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
May 25th, 2013, 01:35 PM
#8
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|