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

Write to text file in reverse order

Status
Not open for further replies.

Kreiss

Programmer
Oct 8, 2003
48
0
0
US
I would like to be able to write to a text file so the new stuff is at the top of the file.
i.e.
INSTEAD OF:
TEST1
TEST2
TEST3
TEST4
TEST5

I WOULD LIKE:
TEST5
TEST4
TEST3
TEST2
TEST1

Not for sure how to accomplish this: Thanks for the help

Here is my existing code
'create reference to Microsoft Scripting Runtime

On Error Resume Next
Dim fso As Object
Dim txtFile As Object
Dim ErrorLogDir As String
ReportDir = "M:\SAPS\TRP\trp_reports\1PrintDetails.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtFile = fso_OpenTextFile(ReportDir, ForAppending)
txtFile.Skip (2)
txtFile.WriteLine "Test9
 
Where is your data coming from. If it is in an array, you could step through it backwards.

For X = 10 to 1 Step -1
Write Line Here
Next
 
If your file is a fixed length file you could use the following code found in this thread:

thread222-689374

Swi
 
I'm writing a date field and error description

Here is my existing code
'create reference to Microsoft Scripting Runtime

On Error Resume Next
Dim fso As Object
Dim txtFile As Object
Dim ErrorLogDir As String
ReportDir = "M:\SAPS\TRP\trp_reports\1PrintDetails.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtFile = fso_OpenTextFile(ReportDir, ForAppending)
txtFile.Skip (2)
txtFile.WriteLine now() & " " strErrDescr
 
The only idea I can come up with is this.

When you need to add a new line, you open a new File1 and write your data to it. While you still have File1 open, you open the Original_File, read it in, and write it out to File1.

After all is read and written, close both files. Kill Original_File and rename your new File1 as Original_File.

This will work, but if you have lots of data in the Original_File, or you are writing often, I think it will become a problem.

Maybe someone else will come up with a better idea.
 
Usually you want logging operations to be fairly cheap since they are done so often. It would make sense to simply append new entries to the end of the logfile.

If it is necessary to present the data in another sequence it makes sense to reverse it or sort it as part of the reporting and viewing process instead. It doesn't take much of a program or even script to do this, though there is so little demand for it that Windows doesn't come with a "record sequence reversal" utility, though there is of course the command line SORT.EXE tool.
 
Thanks for the responses.

Kacy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top