-
October 29th, 2001, 07:12 AM
#1
Files
Hello,
Is there any function in vb that can bring me the last line of a text file?
Right now i read line by line. It's ok for little file but not for big file.
tnx
Go scuba diving...
-
October 29th, 2001, 11:20 AM
#2
Re: Files
There is not that kind of function as far as I know, but
John "The King" G. Duffy showed the fastest way to read a file
http://codeguru.com/cgi-bin/bbs/wt/s...age=&view=&sb=
if file is really big, this may not work fine,owever it is worth a try
You can read the whole file in a variable and then strip last line from
variable (which is the line that from back to top starts with a carriagereturn)
So you can use the "instrRev" function to find first vbcrlf in the big var
resulting from that kind of reading and use the right$ function to
extraxt the line you're looking for.
Hope this may help
Cesare Imperiali
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
October 29th, 2001, 12:16 PM
#3
Re: Files
Actually there is a better way to find the last line in a file but it still involves reading the file. Here is a sample using a RichTextBox.
The primary purpose of this sample was for a Row/Column counter. but is can be used for anything. the COmmand1 routine will load a fiel then msgbox the last line
For the sample, Add a Richtextbox (RTF) two text boxes txtRow, TxtCol, and a command button (Command1). Paste this code click the command button
private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (byval hwnd as Long, byval wMsg as Long, byval wParam as Long, byval lParam as Any) as Long
Const EM_LINEINDEX = &HBB ' get offset of current line
'------------ Called any time that the I
' -Beam moves on the screen -------------
private Sub Command1_Click()
Dim X
rtf.LoadFile "C:\Autoexec.Bat"
rtf.SelStart = len(rtf.Text) - 1
rtf.Refresh
X = rtf.SelStart - SendMessage(rtf.hwnd, EM_LINEINDEX, -1, 0&) + 1
' display last line of text file
MsgBox mid(rtf.Text, len(rtf.Text) - X)
End Sub
private Sub rtf_SelChange()
txtRow = rtf.GetLineFromChar(rtf.SelStart) + 1 ' get the current line
txtcol = rtf.SelStart - SendMessage(rtf.hwnd, EM_LINEINDEX, -1, 0&) + 1
End Sub
John G
-
October 29th, 2001, 12:18 PM
#4
Re: Files
See my response to this post for a better mousetrap
John G
-
October 29th, 2001, 12:52 PM
#5
Re: Files
Very nice, John!
Iouri Boutchkine
iouri@hotsheet.com
-
October 30th, 2001, 03:45 AM
#6
Yes, nice!
Thanks, John "TheKing" G. Duffy
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
October 30th, 2001, 08:58 AM
#7
Re: Files
<de-lurk>
Here's a simple way - it uses the 'Seek' command in VB to go to an assigned distance from the end of the file, then reads in the rest of the file and looks for the 'VBCRLF' :
Code:
private Function GetLastLine(byval FileName as string) as string
Dim iFile as Integer
Dim lFileLen as Long
Dim sBuffer as string
Dim iPos as Integer
'
' You might need to play with the offset depending on how big
' your 'lines' usually are in the file
'
Const OFFSET as Long = 160
'
' The line delimiter string - in this case I'm using VBCRLF, it
' could just be LF in some cases, again, be careful
'
Const FINDSTR as string = vbCrLf
'
iFile = FreeFile
sBuffer = Space$(OFFSET)
'
Open FileName for binary Access Read as #iFile
seek #iFile, (LOF(iFile) - OFFSET)
get #iFile, , sBuffer
Close #iFile
'
iPos = InStrRev(sBuffer, FINDSTR)
If iPos = 0 then
GetLastLine = sBuffer
else
GetLastLine = mid$(sBuffer, iPos + len(FINDSTR))
End If
'
End Function
- of course, you might want to put the 'seek' part inside a loop if the line is longer than your OFFSET value - use at your own risk.
</de-lurk>
Chris Eastwood
VBCodeLibrary - http://www.vbcodelibrary.com
Last edited by Cimperiali; July 22nd, 2004 at 04:54 PM.
-
October 30th, 2001, 09:05 AM
#8
Re: You!!!
You're here, you answer (and a Top one, too), and I am already out of votes!!
Cruel life!!
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
October 30th, 2001, 09:08 AM
#9
Re: You!!!
<de-lurk>
You didn't see 'nuffin.
Move along now.... :wink
</de-lurk>
Chris Eastwood
VBCodeLibrary - http://www.vbcodelibrary.com
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
|