Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Log File 1

Status
Not open for further replies.

MickeD

Programmer
Feb 20, 2000
54
Hi,
How can I insert row at the Top of text file?
(Something like Error Log - Last ocurred error - on the first line of file)
I try this way:
Code:
For i = 1 To 5
    f = FreeFile
    Open "C:\Errors.log" For Random Access Write As #f
    Seek #f, 1
    txt = "[" & Now() & "]  Line #" & CStr(i)
    Put #f, , txt + vbCrLf
    Close #f
Next i

but this code overwrite the first line.
Help please
 
I would do something like:
[tt]
For i = 1 To 5
[tab]f = FreeFile
[tab]Open "C:\Errors.log" For Binary As #f
[tab]txt = "[" & Now() & "] Line #" & CStr(i) & vbCrLf
[tab]OldTxt = String$(Lof(f), 32)
[tab]Get #f, 1, OldTxt
[tab]txt = txt + OldTxt
[tab]Put #f, 1, txt
[tab]Close #f
Next
[/tt]

Are you repeating the write for testing purposes? "Line #5" will be the first line of the file when you are finished, followed by "Line #4", etc.

Don't worry that I changed your file access method to binary. Random access isn't suited for appending to the beginning of a file. After closing the binary open you can always reopen it as random to allow access to individual lines.

Hope this helps.
VCA.gif

Alt255@Vorpalcom.Intranets.com
 
Thanks a lot!
It's work excellent!

But I think there must be File size checking before putting all the file into variable.
What's a maximum size?
 
The size limitation on a variable length sting is 2 billion characters. Will your log files exceed that size?

Try this code. It will create a 1000000 byte file in the current directory:
[tt]
A$ = String$(1000000, 32)
f = FreeFile
Open "test.bin" For Binary As #f
Put #f, 1, A$
Close
MsgBox FileLen("test.bin")
[/tt]

VCA.gif

Alt255@Vorpalcom.Intranets.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top