Inserting a line at the beginning of a Text file

A

Alankar

Hi All,

I have recently exported out a text file from my access db, and after
exporting the data to the text file, i then append a variable to that
text file. To do this i use the following code

Dim hFile As Integer ' File handle

hFile = FreeFile ' first free file handle
Open stFilename For Append As #hFile
Print #hFile, stRecCount
Close #hFile

However with the Append command, that means it appends my variable
stRecCount at the end of the rest of the records, what I would like to
do is to Insert that variable as the very first line and then have the
records below that.

Basically my question is how do I insert stRecCount at the beginning
of a text file?

Thanks
 
B

Brendan Reynolds

What you'd have to do is insert your new line, followed by the contents of
the old text file, into a new text file. Afterward you can, if necessary,
delete or rename the old file (renaming is obviously safer) then rename the
new file with the old name.

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
A

Alankar

Thanks however its not the logic of what needs to be done that i need,
its the code I am looking for.

Basically what currently happens, is a query is ran and a table is
created, this table is then exported out to a textfile called
"nameoftextfile & date & Time stamp" using the TransferText Command.

The next thing that happens is that I open that text file up to add a
variable to it, however I can only either overwrite the file using the
Output method therefore losing the original table that was exported
and the file only contains the variable, or I can Append to the text
file, however append adds the variable as the last line in the file
and I need the variable to be the first line in the file.

This is due to the fact that this file while then be used by another
program that requires the text file to be in a certain format,
starting with the variable and then the contents of the table.
 
B

Brendan Reynolds

Public Sub AppendStart()

Dim intSource As Integer
Dim intTarget As Integer
Dim strSource As String
Dim strTarget As String
Dim strLine As String

strTarget = "C:\Test1\NewFile.txt"
strSource = "C:\Test1\Test.txt"
intSource = FreeFile
Open strSource For Input As intSource
intTarget = FreeFile
Open strTarget For Output As intTarget
Print #intTarget, "New Line Here"
Do Until EOF(intSource)
Line Input #intSource, strLine
Print #intTarget, strLine
Loop
Close #intSource
Close #intTarget
Name strSource As "C:\Test1\Backup.txt"
Name strTarget As "C:\Test1\Test.txt"
Shell "Notepad.exe C:\Test1\Test.txt"

End Sub


--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
A

Alankar

Hi Brendan,

Thanks, that worked a treat, should sort a load of my problems out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top