Sign in to follow this  
Followers 0
DickG

Apending text to top of file

7 posts in this topic

I would like to write a line to the top of a text file instead of the end of the file. The reason is that I want to use ListView to display the content with the newest info at the top so I don't have to scroll all the way down to see the last entry.

Is there an efficient way to do this?

Share this post


Link to post
Share on other sites



I would like to write a line to the top of a text file instead of the end of the file. The reason is that I want to use ListView to display the content with the newest info at the top so I don't have to scroll all the way down to see the last entry.

Is there an efficient way to do this?

$MYTEXT = "Blahblahblah"

$Read = FileRead(The File)

FileDelete(The File)

FileWrite($MYTEXT & @CRLF & $Read

Share this post


Link to post
Share on other sites

Easier done with an INI Write.

But if you are using FileOpen and then, Filewrite or FileWriteLine, then you need to read the file into memory (Array) write your new line and then write back the read values/lines.

I would like to write a line to the top of a text file instead of the end of the file. The reason is that I want to use ListView to display the content with the newest info at the top so I don't have to scroll all the way down to see the last entry.

Is there an efficient way to do this?

Share this post


Link to post
Share on other sites

I would like to write a line to the top of a text file instead of the end of the file. The reason is that I want to use ListView to display the content with the newest info at the top so I don't have to scroll all the way down to see the last entry.

Is there an efficient way to do this?

Hrm. Offhand...

$FileString = $StringToWrite&(FileRead(MyFile.txt))
FileOpen("MyFile.txt", 2)
FileWrite("MyFile.txt", $FileString)

Problem is that requires a read and write of the whole file, which may or may not be largish...

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

You could do something like this:

$file=FileOpen("C:\test.txt",2)
$FileText= FileRead($file)
$MyLineOfText= "insert this" & @CRLF
$FileText=$MyLineOfText & $FileText
FileWrite($file,$FileText)
FileClose($file)
Edited by SpookMeister

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I would like to write a line to the top of a text file instead of the end of the file. The reason is that I want to use ListView to display the content with the newest info at the top so I don't have to scroll all the way down to see the last entry.

Is there an efficient way to do this?

A better approach, esp. if the file is not huge, is to simulate a stack, or Last In First Out queue.

You could use the ordinary file functions to append lines, then after reading the file into an

array use _ArrayPop() to get the last line, instead of the first. Like popping off the stack.

Edited by MilesAhead

Share this post


Link to post
Share on other sites

Thanks guys.

I had considered a variation of most of these, but figured that opening and closing the file for each line was inefficient. I was hoping there was some command or function I was not aware of.

But I've since reconsidered how to I should do this. So far, I was setting a ListView, defining the columns and all, then displaying it after loading it from reading a text file. But of course, that puts the last entry at the bottom, just like the text file is.

So I tried something else: I read the text file into an array, then used _ArrayReverse(), then _ArrayDisplay(). That works so much better and faster, and much less code.

BUT, the problem is, I haven't found a way to parse a line of text, which has 7 comma-delimited items, into 7 separate columns in _ArrrayDisplay(). It just shows one column: Col(1). So that sort of implies that there can be more columns, but I haven't found anything on this.

So, is it possible to set up columns in an array to display them as columns?

A better approach, esp. if the file is not huge, is to simulate a stack, or Last In First Out queue.

You could use the ordinary file functions to append lines, then after reading the file into an

array use _ArrayPop() to get the last line, instead of the first. Like popping off the stack.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0