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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Append text to Beginning of File

Status
Not open for further replies.

d1novak

Instructor
Jul 21, 2003
27
US
I have a VBScript that writes error info to a text file. it works but appends to the bottom of the file.

set log = fso_OpenTextFile("C:\cleanMB\CleanMBLog.txt", ForAppending, true)
log.WriteblankLines(2)
log.WriteLine("Success! "&NOw)

How can I append this to the top of the file?
 
I don't think you can.

Instead you could:

Write the new text to a new file
Append the contents of the existing file to the new file Delete the existing file
Rename the new file

Hope this helps.

[vampire][bat]
 
If the log file is huge, which normally is of considerable in size, you can approach the problem by using adodb.recordset with jet.oledb provider and extended properties text. Then you would have insert functionality available to insert the text at the beginning say. Only, there would be a limit on the length of the text line. As a start, you can take a look of a previous thread concerning reading only:
 
Alternatively, you could simply record your log file in reverse, and add the most recent entries to the END of the log. That's the way it is usually done.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
After minimum experimentation, my idea posted would not work. I can only get some result using addnew, but only to the bottom! not at the top. Hence, a revised plan would be to import the text to disconnected rs and insert new info at the top and then export to a text file. But it would be more script lines. In any case, I would take back the idea as of presented in my previous post.
 
I'd use the following: open the log file > read all the contents > write over the previous file: the new lines + all the existing contents. I've tryed it and it works. ;)

Here's how it looks:

Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")

Set f = fso_OpenTextFile("test.txt", 1)
ReadAllTextFile = f.ReadAll

Set f = fso_OpenTextFile("test.txt", 2, True)
f.WriteLine("Blaaa")
f.WriteLine("Blaaaa some more...")
f.Write(ReadAllTextFile)

msgbox "Done!
 
hallymaster, there is a power cut immediately after:

f.WriteLine("Blaaa")

where is the rest of the file.

That's why I feel that my approach (slightly more tedious) is much safer.


Hope this helps.

[vampire][bat]
 
If you use MS Word you could append things to the top with this code.

Code:
wdStory = 6
wdMove = 0
Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set objDoc = objWord.Documents.Open("c:\scripts\testdoc.doc")

Set objSelection = objWord.Selection
objSelection.HomeKey wdStory, wdMove

objSelection.TypeText "This text was appended to an existing Word document."
objSelection.TypeParagraph()
objSelection.TypeParagraph()
 
earthandfire, I didn't find any "power cut". I've tested that script and it does its job. If you want to test it, just paste the code into a .vbs file, create a non-empty text.txt (it has to have some text in it!) in the same folder as the .vbs and run the .vbs. On my computer (XP SP2) the text.txt is updated with the new lines. Wasn't that the problem we're answering to?
 
hallymaster, my point was what if there was a power cut or some other interruption before the writing had finished - the entire file would be lost.

I never disputed that your code works, that wasn't the issue, my argument was how safe is it to use that approach, when a simple alternative was available.

Sorry if I didn't explain myself clearly before, but whenever I need to "append" text to the beginning of a file I always:

write the new text to a new file
copy the old file to the new file
delete the old file
rename the new file

Other than as a result of a fatal hard disk crash (when everything would almost certainly be lost in any case), one version of the file will always be available.

Hope this helps.

[vampire][bat]
 
Question: why do you want to prepend the text? Isn't it easier just to append it?

If you want to see the latest entry, there is a cygwin/unix utility called tail which will tell you what the latest entry is.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top