How to update a macro with “Append text” to a ”Prepend text” Macro (v11.0.4)

Update

There is a working macro that prepends but it is built on RegEx alone.

So I can manage this 'prepending issue'.

It's only that there doesn't seem to be any example macro showing how the Prepend action works.


Aim
How can the new Prepend action be employed for a basic example macro.

The macro below is only an example, a basic macro will be simpler.
To prepend a TEXT containing FILENAME, PATH and BODYtext from som other file for reference to an existing textfile ("ReadingList").

The Wiki did not really help.

/okn

RL2a TTL & PATH -Text File ⇢ Reading List 2025 copy.kmmacros (28 KB)

But how to do it with the Prepend action?

PS: A related working marco that gives a Clipboard is mentioned in post 17
@ Improving on the Text Reference Macro - Test if Clb is empty Macro (v11.0.4) - #17 by Nige_S

/okn

Prepend gets less and less efficient as file size grows -- you have to read in the entire file, put your new text at the start, write the new version back out again. Much more work than simply adding some lines to the end of the file (which is why logs are appended to, not prepended).

Because of that there is no "Append Text to File" "Prepend Text to File" Action (thanks to @okn for the correction!). You'll have to either read the file into a variable, append text to that, then write the new contents out to disk again:

...or use @ccstone's "Search and Replace" trick:

"Search for the very beginning (\A) of the string read in from the file and replace the 0th character with the contents of Local_textToAdd then two newlines. Write the new string out to the source file, replacing the old contents."

Prepend gets less and less efficient as file size grows

I suspected this could be a problem!

Because of that there is no "Append Text to File" Action.

You wanted to write "Prepend Text to File" Action :slight_smile:

Re: @ccstone's macro

Write the new string out to the source file, replacing the old contents."

Doesn't this mean that this macro will read the whole text, too?

Just to make sure, in which case it has the same drawback as the 1st version (%Variable%Local_pathToFile%)

Much more work than simply adding some lines to the end of the file (which is why logs are appended to, not prepended).

Why I wanted the Prepend is simply to see the last/ newest entry when opening the file without having to scroll down, but then it occured to me that I could just add the ”Arrow down” action after the Append action,

what do you think?

/OmarKN

Indeed, and now corrected -- thanks!

Yes. But that kind of single-Action text manipulation is pretty quick, even for large text files.

There is no way round it -- when prepending to a simple text file you have read in and then write out the entire thing.

Certainly the easier choice!

But try the read/append/write routine for yourself. With modern machines and, particularly, SSD storage you'll find it takes no time at all to prepend to even a large plain text file.

1 Like