Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default read last line of a file

Is there a quick way of reading the last line of a plain text file
without having to open it and read each line.

I don't think I can use any of the fancy extensions such as binary mode
or whatever, due to the file being plain text
--
Mike
Please post replies to newsgroup to benefit others
Replace dead spam with ntl world to reply by email
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 44
Default read last line of a file

If You write to a file in Random mode, You can use Seek
statement to find last record.
For example:

Type Record ' Define user-defined type.
ID As Integer
Name As String * 20
End Type

Dim MyRecord As Record, MaxSize, RecordNumber ' Declare
variables.
' Open file in random-file mode.
Open "TESTFILE" For Random As #1 Len = Len(MyRecord)
MaxSize = LOF(1) \ Len(MyRecord) ' Get number of records
in file.
' The loop reads all records starting from the last.
For RecordNumber = MaxSize To MaxSize - 1
Seek #1, RecordNumber ' Set position.
Get #1, , MyRecord ' Read last record.
Next RecordNumber
Close #1 ' Close file.

But, if You use other mode to read data in text file, I
don't know best way to do this. Try it:

Function ReadLastLine(fName As String) As String
Dim fNum As Long
Dim strtemp As String

fNum = FreeFile()
Open fName For Input As #fNum
Do While Not EOF(fNum) 'ignore all line, not last
Line Input #fNum, strtemp
Loop
Close #fNum

ReadLastLine = strtemp

End Function




-----Original Message-----
Is there a quick way of reading the last line of a plain

text file
without having to open it and read each line.

I don't think I can use any of the fancy extensions such

as binary mode
or whatever, due to the file being plain text
--
Mike
Please post replies to newsgroup to benefit others
Replace dead spam with ntl world to reply by email
.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default read last line of a file

Hi,
Do you know in advance how the files i structured? eg it's created by you,
or is it a standard text file intended to read?
If not intended to read, i stronly recomend to use an other format like
random acccess ilo text mode.
If you still have to use text mode there is no better way than to loop
through the file untill EOF is reported, maybe LineInput might be faster...

/Jonas

"Mike" skrev i meddelandet
...
Is there a quick way of reading the last line of a plain text file
without having to open it and read each line.

I don't think I can use any of the fancy extensions such as binary mode
or whatever, due to the file being plain text
--
Mike
Please post replies to newsgroup to benefit others
Replace dead spam with ntl world to reply by email



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default read last line of a file

On Sun, 23 Nov 2003 at 01:14:35, Jonas Caspersson (Jonas Caspersson
) wrote:
Do you know in advance how the files i structured? eg it's created by you,
or is it a standard text file intended to read?
If not intended to read, i stronly recomend to use an other format like
random acccess ilo text mode.
If you still have to use text mode there is no better way than to loop
through the file untill EOF is reported, maybe LineInput might be faster...

Just realised the lines in the files are of fixed length - so can use
losmac's method once I have determined the length

I need them to be flat file format for something else that reads those
files
--
Mike
Please post replies to newsgroup to benefit others
Replace dead spam with ntl world to reply by email
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 599
Default read last line of a file

Mike

Here's a sub I use to do that

Sub finddstamp()

Dim Fname As String, LastLine As String
Dim Fnum As Integer, i As Integer
Dim CarRet As String

Fname = "C:\Dick\Tester\FindLast.doc"
Fnum = FreeFile

Open Fname For Input As Fnum

For i = LOF(Fnum) To 1 Step -1
Seek #Fnum, i
CarRet = Input(1, #Fnum)
If CarRet = vbCr Or CarRet = vbCrLf Then
Line Input #Fnum, LastLine
MsgBox LastLine
Close Fnum
Exit Sub
End If
Next i

End Sub

--
Dick Kusleika
MVP - Excel
www.dicks-clicks.com
Post all replies to the newsgroup.

"Mike" wrote in message
...
Is there a quick way of reading the last line of a plain text file
without having to open it and read each line.

I don't think I can use any of the fancy extensions such as binary mode
or whatever, due to the file being plain text
--
Mike
Please post replies to newsgroup to benefit others
Replace dead spam with ntl world to reply by email





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can Macro read line graphs? WH99 Excel Discussion (Misc queries) 2 October 10th 08 03:51 PM
Can Macro read line graphs? WH99 Charts and Charting in Excel 2 October 10th 08 03:49 PM
I have a read only xl file, I need it to be read and write drama queen Excel Discussion (Misc queries) 3 July 1st 06 12:25 AM
How can a file be converted from Read-Only to Read/Write Jim in Apopka Excel Discussion (Misc queries) 2 November 19th 05 04:59 PM
How do I read values from a line chart? Lynn Charts and Charting in Excel 3 October 9th 05 09:11 PM


All times are GMT +1. The time now is 10:39 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"