Replace All Text In File

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
scriptor2016
Posts: 860
Joined: 21 Dec 2015, 02:34

Replace All Text In File

20 Mar 2019, 00:52

Greetings

I am trying to:

1. read all contents in an existing .txt file
2. wipe out all said contents, and replace them with new contents
3. update/save the file

How do I do this without deleting the file? So far I have come up with this script but I admit, I have been scratching my head over this one for months on and off:

Code: Select all

FileRead, Contents, C:\MyTextFile.txt ;read all text in the file
StringReplace, NewTextVar, Contents, %Contents%, ThisIsTheNewText, All ;assign "ThisIsTheNewText" as the new text to be written to the file
Contents= ;wipe out all original text in the file
FileAppend, %Contents%%NewTextVar%, C:\MyTextFile.txt ;write the new text to the file
Return
I am really out to lunch here. What am I doing wrong here?
Rohwedder
Posts: 7647
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Replace All Text In File

20 Mar 2019, 01:36

Hallo,
i recommend to delete the file!
But if you are a masochist: https://autohotkey.com/docs/objects/File.htm
Kobaltauge
Posts: 264
Joined: 09 Mar 2019, 01:52
Location: Germany
Contact:

Re: Replace All Text In File

20 Mar 2019, 06:54

The documentation recommends deleting the file before.
But you can use the file object like Rohwedder wrote. And instead for appending you overwrite the file.

Code: Select all

file := FileOpen("MyTextFile.txt", "w") 
file.write()
file.close()
electrone77
Posts: 16
Joined: 16 Mar 2019, 12:30

Re: Replace All Text In File

20 Mar 2019, 09:25

you might wanna try it with tf.ahk, its a lib you need to download, however i am not sure how it handles the files.
Heres the info
download
example codes (see 1st code there is "!" before File.txt. i guess it overwrites the file):

Code: Select all

TF_ReplaceInLines("!File.txt","1,3,9","","key","lock") ;  update source file, replace "key" with "lock" in lines 1, 3 and 9

Code: Select all

TF_RegExReplace("File.txt","im)^(.*)$","[$1]") ; pass on a file, wrap all lines in []
or

Code: Select all

F=!file.txt 
TF_Replace(F, "this", "that") ; Replace the word "this" with "that" and overwrite original file.txt
Last edited by electrone77 on 20 Mar 2019, 10:22, edited 1 time in total.
scriptor2016
Posts: 860
Joined: 21 Dec 2015, 02:34

Re: Replace All Text In File

20 Mar 2019, 10:01

Thanks guys! I was able to use a combination of file.write() and fileappend to get the results I needed.

I had thought it would be an easy task to just read the text file, replace the text, then overwrite the file - but I had to search long and hard for information on how to do it. I was hoping to avoid deleting the file and then saving a new one - my thinking was that accessing the hard drive twice just for the one task would cause a mini-delay that might cause the rest of my script to misfire. However, it IS possible and now it works :)
scriptor2016
Posts: 860
Joined: 21 Dec 2015, 02:34

Re: Replace All Text In File

20 Mar 2019, 13:22

I ended up going with this

Code: Select all

FileRead, MyContents, C:\MyTextFile.txt ;this reads whatever text is in the text file we are working with
file := FileOpen("C:\MyTextFile.txt", "w") ;this wipes out all text in the file - does it overwrite it in this line as well? Either way, it works as planned
file.close() ;this closes the file - at this stage, the text file is empty and overwritten (saved) to the hard drive
FileAppend, ThisIsTheNewText, C:\MyTextFile.txt ;this writes "ThisIsTheNewText" to the text file (without the quotes)
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: Replace All Text In File

20 Mar 2019, 13:44

scriptor2016 wrote:
20 Mar 2019, 13:22
I ended up going with this

Code: Select all

FileRead, MyContents, C:\MyTextFile.txt ;this reads whatever text is in the text file we are working with
file := FileOpen("C:\MyTextFile.txt", "w") ;this wipes out all text in the file - does it overwrite it in this line as well? Either way, it works as planned
file.close() ;this closes the file - at this stage, the text file is empty and overwritten (saved) to the hard drive
FileAppend, ThisIsTheNewText, C:\MyTextFile.txt ;this writes "ThisIsTheNewText" to the text file (without the quotes)
Thanks for sharing your solution scriptor2016 :) I had been pondering this issue this week
-TL
scriptor2016
Posts: 860
Joined: 21 Dec 2015, 02:34

Re: Replace All Text In File

20 Mar 2019, 23:14

after studying this some more, this is another way to do it:

Code: Select all

FileRead, MyContents, C:\MyTextFile.txt ;read the contents of the text file
file := FileOpen("C:\MyTextFile.txt", "w") ;this wipes out all text in the file and overwrites it as a blank .txt file
file.write("ThisIsTheNewText") ;this writes "ThisIsTheNewText" to the text file (without the quotes)
file.close() ;this closes the file
So I guess that this script still accesses the hard drive twice: once to overwrite the .txt file as a bkank file (in line 2), and then the second time to write the new text to the file (in line 3). I guess there's no way to do it in one go.

I thought maybe the script could

a) read the text file as a variable
b) clear the variable
c) assign the new text to the variable
d) write the variable to the text document and overwrite the file

--therefore accessing the hard drive only once.

Either way, the above code works as well :)
Kobaltauge
Posts: 264
Joined: 09 Mar 2019, 01:52
Location: Germany
Contact:

Re: Replace All Text In File

21 Mar 2019, 00:44

Thank you for testing.
Just learnd that the line

Code: Select all

file := FileOpen("MyTextFile.txt", "w")
overwrites the file. I thought it opens only the handler and

Code: Select all

file.write()
was needed to overwrite with an empty line.
User avatar
labrint
Posts: 379
Joined: 14 Jun 2017, 05:06
Location: Malta

Re: Replace All Text In File

16 Apr 2020, 02:54

Code: Select all

file := FileOpen("MyTextFile.txt", "w")

This code could be the most beautiful one liner I have seen. Basically it saves you the trouble of FileDelete before appending. Imagine you have a file you need to update specifically positioned on the desktop and don't want it moved, FileDelete would move it. This doesn't. Does not require tf library. Beautiful. You little genius you.
TrackQser
Posts: 1
Joined: 24 Dec 2020, 12:32

Re: Replace All Text In File

27 Dec 2020, 08:53

@scriptor2016

Your code works for me, too.

But I am not sure if it is reliable.

Initially "overwriting" the file with the FileOpen "w"-option all file contents are lost if the app crashes before the file.write().

I hoped AHK would offer a function to accomplish this in one step ...
BartmanEH
Posts: 23
Joined: 01 Apr 2021, 15:48

Re: Replace All Text In File

29 Apr 2021, 08:43

scriptor2016 wrote:
20 Mar 2019, 23:14

Code: Select all

FileRead, MyContents, C:\MyTextFile.txt ;read the contents of the text file
file := FileOpen("C:\MyTextFile.txt", "w") ;this wipes out all text in the file and overwrites it as a blank .txt file
file.write("ThisIsTheNewText") ;this writes "ThisIsTheNewText" to the text file (without the quotes)
file.close() ;this closes the file
[...]
Either way, the above code works as well :)
When I try this method on a Windows10 VM, the FileOpen fails every time--even with a sleep, 1000 before it. The only thing that works for me is:

Code: Select all

FileRead, currentLog, log.txt       			;read current log data from file
currentLog := newLogData . "`n" . currentLog
;sleep, 1000
;file := FileOpen(log.txt, "w")      			;wipes out all content in the file and overwrites it as an empty file or creates file (fails!)
;file.write(currentLog)                      	;writes to the file
;file.close()                                	;closes the file
FileDelete, log.txt								;alternate method that works part 1/2
FileAppend, %currentLog%, log.txt				;alternate method that works part 2/2

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, mikeyww, PsysimSV, RussF and 296 guests