How to append text to the beginning of the text file

  • Thread starter Thread starter Peter Afonin
  • Start date Start date
P

Peter Afonin

Hello,

I'm using an Access project to create a table and then to export it to the
text file. Then I need to write three lines of text at the beginning of this
file.

This code works fine for me:

Dim fs As New Scripting.FileSystemObject
Dim ts As TextStream
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile("\\Win2000\Everyone\XeroxExport\" & Me.txtForm &
".txt", ForAppending)
ts.WriteLine ("This is a test 1.")
ts.WriteLine ("This is a test 2.")
ts.WriteLine ("This is a test 3.")

The only problem that if I use this code it writes these lines at the end of
the text file, and I need these lines to be at the beginning. What code
should I use?

I would appreciate your help.

Thank you,
 
AFAIK, that's the way it is. If you want to append to the beginning, you'll
have to write to a new file, read the contents of the old file and write it
to the new file, delete the old file and rename the new file to the old file
name.

By the way, you don't need to declare fs as New and then create it.

Dim fs As New Scripting.FileSystemObject

is sufficient, although I feel the following is preferable:

Dim fs As Scripting.FileSystemObject
Set fs = New Scripting.FileSystemObject

or

Dim fs As Scripting.FileSystemObject
Set fs = CreateObject("Scripting.FileSystemObject")
 
Thank you, Doug.

This is quite an exercise.

Peter

Douglas J. Steele said:
AFAIK, that's the way it is. If you want to append to the beginning, you'll
have to write to a new file, read the contents of the old file and write it
to the new file, delete the old file and rename the new file to the old file
name.

By the way, you don't need to declare fs as New and then create it.

Dim fs As New Scripting.FileSystemObject

is sufficient, although I feel the following is preferable:

Dim fs As Scripting.FileSystemObject
Set fs = New Scripting.FileSystemObject

or

Dim fs As Scripting.FileSystemObject
Set fs = CreateObject("Scripting.FileSystemObject")

--
Doug Steele, Microsoft Access MVP

(No private e-mails, please)


Peter Afonin said:
Hello,

I'm using an Access project to create a table and then to export it to the
text file. Then I need to write three lines of text at the beginning of this
file.

This code works fine for me:

Dim fs As New Scripting.FileSystemObject
Dim ts As TextStream
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile("\\Win2000\Everyone\XeroxExport\" & Me.txtForm &
".txt", ForAppending)
ts.WriteLine ("This is a test 1.")
ts.WriteLine ("This is a test 2.")
ts.WriteLine ("This is a test 3.")

The only problem that if I use this code it writes these lines at the
end
 
I'm not sure about FSO, but if you use the plain vanilla VBA Open Statement
and open the file as Binary, you should be able to read the entire file in
one pass and then write it out in one.

Dim intFreeFile As Integer
Dim strBuffer As String

intFreeFile = FreeFile
Open FullPathToFile For Binary Access Read As intFreeFile

strBuffer = String(FileLen(FullPathToFile), " ")

Get #intFreeFile, , strBuffer

should read the entire file in one pass, while

Put #intFreeFile, , strBuffer

should write it out in one pass. (Of course, you'll need a slightly
different Open statement for the output file: the Access Read would be
Access Write)

(WARNING: This is untested aircode!)


--
Doug Steele, Microsoft Access MVP

(No private e-mails, please)


Peter Afonin said:
Thank you, Doug.

This is quite an exercise.

Peter

Douglas J. Steele said:
AFAIK, that's the way it is. If you want to append to the beginning, you'll
have to write to a new file, read the contents of the old file and write it
to the new file, delete the old file and rename the new file to the old file
name.

By the way, you don't need to declare fs as New and then create it.

Dim fs As New Scripting.FileSystemObject

is sufficient, although I feel the following is preferable:

Dim fs As Scripting.FileSystemObject
Set fs = New Scripting.FileSystemObject

or

Dim fs As Scripting.FileSystemObject
Set fs = CreateObject("Scripting.FileSystemObject")

--
Doug Steele, Microsoft Access MVP

(No private e-mails, please)


of
this
Me.txtForm
 
Douglas J. Steele said:
I'm not sure about FSO, but if you use the plain vanilla VBA Open
Statement and open the file as Binary, you should be able to read the
entire file in one pass and then write it out in one.

Dim intFreeFile As Integer
Dim strBuffer As String

intFreeFile = FreeFile
Open FullPathToFile For Binary Access Read As intFreeFile

strBuffer = String(FileLen(FullPathToFile), " ")

Get #intFreeFile, , strBuffer

should read the entire file in one pass, while

Put #intFreeFile, , strBuffer

should write it out in one pass. (Of course, you'll need a slightly
different Open statement for the output file: the Access Read would be
Access Write)

(WARNING: This is untested aircode!)

For text files, to read the whole file in one go I usually use code like
this:

Dim strFileName As String
Dim intFileNo As Integer

strFileName = "C:\Path To\TheFile.txt"

intFileNo = FreeFile()
Open strFileName For Input As #intFileNo
Me.txtFileData = Input(LOF(intFileNo), intFileNo)
Close #intFileNo
 
Dirk Goldgar said:
For text files, to read the whole file in one go I usually use code like
this:

Dim strFileName As String
Dim intFileNo As Integer

strFileName = "C:\Path To\TheFile.txt"

intFileNo = FreeFile()
Open strFileName For Input As #intFileNo
Me.txtFileData = Input(LOF(intFileNo), intFileNo)
Close #intFileNo

Thanks, Dirk. I'd trust that more than my aircode!
 
Thank you, Douglas.

I've done it already the way you said in your first message. It turned out
to be easier than I thought.

Peter
 
Back
Top