Replace string if string exists

Get help with using AutoHotkey and its commands and hotkeys
talormanda
Posts: 11
Joined: 03 Oct 2014, 15:17

Replace string if string exists

16 Sep 2019, 14:55

I am trying to replace a line in a file if the string exists, but it is not working correctly. Below is what I have attempted after some hours of attempts. I basically want to see if the text is there, and if it is there, replace it in the file and stop running. I would assume it to be something like......"if text exists - replace the string we want, else - do nothing."

Code: Select all

SearchString = TextToSearch
ReplaceString = TextToReplace
FilePath =  C:\Users\johnsmith\Desktop\filename.xml

Loop, Read, %FilePath%
{

Line := a_LoopReadLine

    If InStr (Line, SearchString)
        {
            MsgBox, Text found

            FileRead, WFCFile, %FilePath%
            StringReplace, NewWFCFile, WFCFile, %SearchString% , %ReplaceString% , All
            FileDelete, %FilePath%
            FileAppend, %NewWFCFile%, %FilePath%
            Break
        }

}

User avatar
flyingDman
Posts: 359
Joined: 29 Sep 2013, 19:01
Location: Burbank, California

Re: Replace string if string exists

16 Sep 2019, 15:27

First StrReplace will only replace if it finds the SearchText. If it's not there, no changes will be made. There is no need to try to find out if the SearchText is there in the first place.
Second, if possible, I would read the entire file in a variable using FileRead rather than using a "loop,read", do the StrReplace, delete the file and use fileappend to recreate the file. (the helpfile says:
To load an entire file into a variable, use FileRead because it performs much better than a loop (especially for large files).
User avatar
Cuadrix
Posts: 204
Joined: 07 May 2017, 08:26

Re: Replace string if string exists

16 Sep 2019, 15:28

Do you wanna replace the whole line, or only part of that line?
Does your text file contain duplicates?
----
talormanda
Posts: 11
Joined: 03 Oct 2014, 15:17

Re: Replace string if string exists

17 Sep 2019, 07:26

I want to replace part of the line, but I can just replace the entire thing if that is easier. There are no duplicates yet, but there could be in the future. How would you get around that? Is there a way to replace just the part of the line that I want, and not have to Delete and Re-create the file?
maaroo
Posts: 18
Joined: 05 Dec 2018, 05:13

Re: Replace string if string exists

17 Sep 2019, 08:12

Hi this is replacing the first found value and then replaces the file with the replaced item.

Code: Select all

SearchString = TextToSearch
ReplaceString = TextToReplace
FilePath =  C:\Users\johnsmith\Desktop\filename.xml

FileRead, WFCFile, %FilePath%
NewWFCFile := StrReplace(WFCFile, SearchString , ReplaceString , Count,1)
;Count is total replacements done
;1 is only replace first time found. -1 is all. 2 is replace 2 times if exist.
FileDelete, %FilePath%
FileAppend, %NewWFCFile%, %FilePath%
talormanda
Posts: 11
Joined: 03 Oct 2014, 15:17

Re: Replace string if string exists

17 Sep 2019, 08:22

maaroo wrote:
17 Sep 2019, 08:12
Hi this is replacing the first found value and then replaces the file with the replaced item.

Code: Select all

SearchString = TextToSearch
ReplaceString = TextToReplace
FilePath =  C:\Users\johnsmith\Desktop\filename.xml

FileRead, WFCFile, %FilePath%
NewWFCFile := StrReplace(WFCFile, SearchString , ReplaceString , Count,1)
;Count is total replacements done
;1 is only replace first time found. -1 is all. 2 is replace 2 times if exist.
FileDelete, %FilePath%
FileAppend, %NewWFCFile%, %FilePath%
Is there any way to check if the string exists before replacing anything? The reason being that I do not want to run the FileDelete / FileAppend every single time if it is not replacing anything.
hd0202
Posts: 145
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: Replace string if string exists

17 Sep 2019, 22:55

Use the variable "Count" after "StrReplace( ... )" to decide if you delete the file and recreate it.

Hubert
Kobaltauge
Posts: 193
Joined: 09 Mar 2019, 01:52
GitHub: MassimoCastell
Location: Germany
Contact:

Re: Replace string if string exists

17 Sep 2019, 23:55

We are discussing are similar task here
https://www.autohotkey.com/boards/viewtopic.php?f=76&t=67980
take a look, probalby there is an inspiration for you.

You could load the file and perform a RegExMatch. If it found something, you can replace it with RegExReplace and do your file delete and save. As I know, there is no possibility to overwrite a file, but I'm not sure at the moment.
maaroo
Posts: 18
Joined: 05 Dec 2018, 05:13

Re: Replace string if string exists

18 Sep 2019, 06:04

Hi Talormanda,

StrReplace parameter 5 should be -1 to replace all TextToSearch strings.

Code: Select all

NewWFCFile := StrReplace(WFCFile, SearchString , ReplaceString , Count,-1)
Its only renew the file when it found something to replace, with the additional "if (Count>0)" condition, to check how much replaces found by using the count parameter and only replace it when this is >0

Code: Select all

SearchString = TextToSearch
ReplaceString = TextToReplace
FilePath =  C:\Users\johnsmith\Desktop\filename.xml

FileRead, WFCFile, %FilePath%
NewWFCFile := StrReplace(WFCFile, SearchString , ReplaceString , Count,1)
;Count is total replacements done
;1 is only replace first time found. -1 is all. 2 is replace 2 times if exist.
if (Count>0)
{
  FileDelete, %FilePath%
  FileAppend, %NewWFCFile%, %FilePath%
}

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], meddoc, thephoenix, Xtra and 219 guests