|
-
September 10th, 2003, 11:25 AM
#1
Last line of text file in string?
How do i go about putting the last line of a text file in a string?
-
September 10th, 2003, 01:39 PM
#2
Dim LineInString as string
open FNAME foro intut as #1
do until eof(1)
line input #1, LineInString
loop
close #1
'now the last line of the file in in LineInString
kevin
Thank you for your support.
-Bartyles & James (circa 1980)
If it helps, rate it 
if it doesn't, well....I tried
Check out Windows Embedded XP. It's XP compontnetized: add only the components you need to build an OS
And in knowing that you know nothing, that makes you the smartest of all.
- Socrates (469 BC - 399 BC)
-
September 10th, 2003, 02:42 PM
#3
'Set Microsoft Scripting Runtime as a reference
The following uses the FileSystemObject and TextStream Object to read from a file. The message box will display the last line of the text file.
Dim fso As FileSystemObject
Dim ts As TextStream
Dim f As Object
Dim strLastLine As String
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile("FilePath\FileName")
Set ts = f.OpenAsTextStream(ForReading)
'Loops through text file capturing each line in the variable
Do While ts.AtEndOfStream <> True
strLastLine = ts.ReadLine
Loop
MsgBox strLastLine
ts.Close
Set ts = Nothing
Set f = Nothing
Set fso = Nothing
-
September 11th, 2003, 01:00 AM
#4
Here is a quick way to do it 
Code:
Dim A$, I&
Open "C:\test.txt" For Binary Access Read As #1
A = String(LOF(1), 0)
Get #1, , A
Close #1
I = InStrRev(A, vbLf)
A = Mid(A, I + 1)
MsgBox A
Please remember to rate the posts and threads that you find useful.
How can something be both new and improved at the same time?
-
September 11th, 2003, 10:56 AM
#5
Hmm
Is there a way to make this more flexible? Like have a variable of int with a line number... and get it to read that line number... and everytime i change the variable it wont matter because it just reads the variable in the line number?
-
September 11th, 2003, 02:52 PM
#6
Check out this code and modify it to suit your need.
http://www.freevbcode.com/ShowCode.Asp?ID=2667
This goes to the line number you specify and deletes it. You can change the code to read it instead.
Cheers,
Laurent
For an aviator, the three best things in life are a good landing, a good orgasm, and a good sh*t. A night carrier landing is one of the few opportunities to experience all three at the same time.
-
September 11th, 2003, 03:15 PM
#7
'Try this.
'If you put a line number in that is not valid you will not get a msg
'Set Microsoft Scripting Runtime as a reference
Dim fso As FileSystemObject
Dim ts As TextStream
Dim f As Object
Dim strLine As String
Dim LineNumber As Double
Dim Result
LineNumber = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile("c:\test1.txt")
Set ts = f.OpenAsTextStream(ForReading)
Result = InputBox("Enter Line Number", "Line Number")
'Loops through text file capturing each line in the variable
Do While ts.AtEndOfStream <> True
strLastLine = ts.ReadLine
If LineNumber = Result Then
MsgBox strLine
Exit Do
End If
LineNumber = LineNumber + 1
Loop
ts.Close
Set ts = Nothing
Set f = Nothing
Set fso = Nothing
-
September 11th, 2003, 06:05 PM
#8
This might help you out. The first line of the file will have an index of 1.
Code:
Private Function GetLine(ByVal Index As Long) As String
Dim A$, I&, S$()
If Index < 1 Then Exit Function
Index = Index - 1
Open "C:\test.txt" For Binary Access Read As #1
A = String(LOF(1), 0)
Get #1, , A
Close #1
S = Split(A, vbCrLf)
I = UBound(S)
If Index > I Then Index = I
GetLine = S(Index)
End Function
Please remember to rate the posts and threads that you find useful.
How can something be both new and improved at the same time?
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
|