I need some help writing an AHK script that does this: I have an XML file that has 12 unique strings that I need to change in order to mass-create some charts. These 12 strings will always exist in the template XML file. I need the script to pull the strings to replace them with from a TXT file or similar.
So basically, it needs to have an input to select the template XML file and the TXT file to pull from, then go in the template and find the 12 strings to replace, order through the TXT file one at a time to replace them all, save it, then start a new one from where the last one left off, repeat until the TXT file runs out of lines (there could be anywhere from 50 to 500 lines in the TXT file).
Any chance someone could help with this? Thanks a ton!!
Need some help writing a Find & Replace script
- Delta Pythagorean
- Posts: 627
- Joined: 13 Feb 2017, 13:44
- Location: Somewhere in the US
- Contact:
Re: Need some help writing a Find & Replace script
My mind, it can't handle this amount of back and fourth movement @.@
[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat
Re: Need some help writing a Find & Replace script
Ok, maybe this would help visualize it a bit more, I have written a layman's version of the code I need:
Code: Select all
i=1 //counter used for main loop
n=1 //counter used for save loop
MainLoop:
loop until i=12 //there are 12 units on each chart
{for each FindVar in FIND.txt //each "FindVar" is represented by a line of text in FIND.txt
fCt=count the number of instances FindVar apprears in CHART.xml
ReplaceVar=get matching ReplaceVar from REPLACE.txt //find the next line in REPLACE.txt
loop %fCt% //do this section as many times as the FindVar appears in CHART.xml
{starting from beginning, find next FindVar in CHART.xml
replace FindVar with ReplaceVar
return}
if i=12 then goto SaveLoop //if we have reached 12 units, save the file
else i=i+1 //otherwise, increase counter by 1 and repeat
return}
SaveLoop:
save CHART.xml as CHART%n%.xml //saves a new file with incremental numbers at end of file name
close CHART.xml without saving //reverts to its original state
n=n+1 //adds 1 to the filename counter
i=1 //resets main loop
goto MainLoop //begins a new cycle of main loop
- FanaticGuru
- Posts: 1907
- Joined: 30 Sep 2013, 22:25
Re: Need some help writing a Find & Replace script
Couldn't the Find/Replace txt file just be one file with the Find and Replace combos on one line separated by a delimiter like tab:
That seems simpler.
If I am understanding correctly what you want is very doable and probably simpler than your pseudo-code. If I get time I will see what I can do.
FG
Code: Select all
tree apple
furniture table
computer PC
children Mary, Todd, and Suzy
etc.
If I am understanding correctly what you want is very doable and probably simpler than your pseudo-code. If I get time I will see what I can do.
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Re: Need some help writing a Find & Replace script
I suppose it could be, only thing is the FIND text will always be the same static values as the CHART template. The REPLACE text is the only part that will change over time, so I guess the FIND values could just be worked into the code instead of outsourcing it to a txt file. Thanks in advance!FanaticGuru wrote:Couldn't the Find/Replace txt file just be one file with the Find and Replace combos on one line separated by a delimiter like tab:That seems simpler.Code: Select all
tree apple furniture table computer PC children Mary, Todd, and Suzy etc.
If I am understanding correctly what you want is very doable and probably simpler than your pseudo-code. If I get time I will see what I can do.
FG
- FanaticGuru
- Posts: 1907
- Joined: 30 Sep 2013, 22:25
Re: Need some help writing a Find & Replace script
Code: Select all
;~ Get the Information from a Haystack and a Needle File
;~ if the file paths do not change they could be hardcopied without use FileSelectFile
;~ "Loop, Files, FilePattern" could be used to have it Loop through all template XML based on a folder or pattern
FileSelectFile, Haystack_Path,,%A_Desktop%\Test,Haystack
FileSelectFile, Needle_Path,,%A_Desktop%\Test,Needle
FileRead, Haystack_Text, %Haystack_Path%
FileRead, Needle_Text, %Needle_Path%
;~ Loop through the Needle_Text and Search / Replace in Result
Result := Haystack_Text
Loop, Parse, Needle_Text, `n, `r
{
StringSplit, Field, A_LoopField, %A_Tab%
Result := StrReplace(Result, Field1, Field2)
}
MsgBox % Result
Code: Select all
A tree is a great thing
to have in place of furniture
when you went to eat food
Code: Select all
tree mature apple tree
furniture a table
food an apple
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Re: Need some help writing a Find & Replace script
Excellent, I will work on this and let you know how it turns out. Thank you so much!FanaticGuru wrote:Expects a Haystack file like this:Code: Select all
;~ Get the Information from a Haystack and a Needle File ;~ if the file paths do not change they could be hardcopied without use FileSelectFile ;~ "Loop, Files, FilePattern" could be used to have it Loop through all template XML based on a folder or pattern FileSelectFile, Haystack_Path,,%A_Desktop%\Test,Haystack FileSelectFile, Needle_Path,,%A_Desktop%\Test,Needle FileRead, Haystack_Text, %Haystack_Path% FileRead, Needle_Text, %Needle_Path% ;~ Loop through the Needle_Text and Search / Replace in Result Result := Haystack_Text Loop, Parse, Needle_Text, `n, `r { StringSplit, Field, A_LoopField, %A_Tab% Result := StrReplace(Result, Field1, Field2) } MsgBox % Result
And a Needle file like this (two fields separate by a tab:Code: Select all
A tree is a great thing to have in place of furniture when you went to eat food
FGCode: Select all
tree mature apple tree furniture a table food an apple
Who is online
Users browsing this forum: Google [Bot] and 153 guests