Replace string in textfile with same name + incrementing number Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Jimay
Posts: 23
Joined: 11 Aug 2020, 05:14

Replace string in textfile with same name + incrementing number

15 Jun 2021, 19:14

I have a textfile that looks like this:

Code: Select all

Directory
Datax
Datay

Directory
xyz
abc

Directory
Bla bla
Bla bla

Etc.
I would like to rename the strings 'Directory' with the same string name + incrementing number. So it should look like this:

Code: Select all

Directory1
Datax
Datay

Directory2
xyz
abc

Directory3
Bla bla
Bla bla

Etc.
Sometimes the textfile can contain 3 strings with the name Directory, but it can also be 10, 20 or over the 100 etc.

I used the following:
1. First load the textfile as a variable.
2. Use RegExReplace to count the number of occurences of the string
3. Then I used StrReplace to find the string 'Directory' and replace it with 'Directory'[incrementing number].
4. Write the result to a new file.

Here is the code:

Code: Select all

FileRead, myvar, inputfile.txt
dummy := RegExReplace(myvar, "Directory", "Directory", OutputVarCount)	; OutputVarCount contains the number of replacements that occurred
newvar := StrReplace(myvar, "Directory", "Directory" []OutputVarCount)
FileAppend, %newvar%, outputfile.txt
Unfortunately the code doesn't work. When I have example 31 occurences of the string 'Directory', all are renamed 'Directory31'. I can't fix this using a loop. Is there a easier way with only RegExReplace? I searched the forum, but couldn't find an increment solution with RegExReplace.
User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Replace string in textfile with same name + incrementing number  Topic is solved

15 Jun 2021, 19:54

Code: Select all

n := 0
While pos := RegExMatch(myvar, "Directory", m, pos ? pos + 9 : 1)
 myvar := RegExReplace(myvar, m, m . ++n,, 1, pos)
MsgBox, 64, Result, %myvar%
sofista
Posts: 650
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Replace string in textfile with same name + incrementing number

15 Jun 2021, 20:08

Another way, not really that different

Code: Select all

SampleText := "
(
Directory
Datax
Datay

Directory
xyz
abc

Directory
Bla bla
Bla bla
)"

while pos := RegExMatch(SampleText, "(?<=\A|\v)Directory", m, A_Index=1?1:pos+StrLen(m)) {
	SampleText := RegExReplace(SampleText, m, m A_Index,, 1, pos)
}

MsgBox, % SampleText
return

/* Output:

Directory1
Datax
Datay

Directory2
xyz
abc

Directory3
Bla bla
Bla bla

 */
Jimay
Posts: 23
Joined: 11 Aug 2020, 05:14

Re: Replace string in textfile with same name + incrementing number

16 Jun 2021, 08:58

Thanks a lot mikeyww! The power of Autohotkey in combination with the RegExMatch and RegExReplace commands are really astonishing.
These commands have a high learning curve, so the help here is really appreceated! :thumbup:

(Also a thanks to sofista for the code!)
User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Replace string in textfile with same name + incrementing number

16 Jun 2021, 09:33

You are welcome. Sofista also had a nice idea about A_Index: you can use that instead of having a separate variable such as n, because the A_Index is the same, as it represents the iteration number. Have your pick!

I agree that this script is a bit tricky to understand well at first. It uses the ability to specify the starting position in the search, as a way to change the next instance of the target string. That position is then tracked and advanced with each iteration. If you're teadrinker, then you can do all of this in a single line somehow! I just stick to the basics of regex.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ht55cd3 and 342 guests