Help with string replace

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
yardley
Posts: 30
Joined: 05 May 2021, 21:16

Help with string replace

21 Jun 2021, 22:26

Hi all, I have the following script

Code: Select all

FileReadLine, gun1id, D:\TEST\nuvee_ps2_usb_guncon1.ini, 10

FileRead, gun1nuvee, % file := "D:\TEST\Gunfighter II - Revenge of Jesse James (Europe)\nuvee_ps2_usb_guncon1.ini"
FileRecycle, %file%

FileAppend, % StrReplace(gun1nuvee, prepend := "Lightgun_left = 1`r`n"
 , gun1id "`n" prepend), %file%
 
FileRead, gun1nuvee, % file := "D:\TEST\Ninja Assault (JPN)\nuvee_ps2_usb_guncon1.ini"
FileRecycle, %file%

FileAppend, % StrReplace(gun1nuvee, prepend := "Lightgun_left = 1`r`n"
 , gun1id "`n" prepend), %file%
 
FileRead, gun1nuvee, % file := "D:\TEST\Time Crisis 3 (USA)\nuvee_ps2_usb_guncon1.ini"
FileRecycle, %file%

FileAppend, % StrReplace(gun1nuvee, prepend := "Lightgun_left = 1`r`n"
 , gun1id "`n" prepend), %file%
Not the most elegant but it's the best I can come up with. This script takes line 10 of the nuvee_ps2_usb_guncon1.ini file and appends it to a number of other files above the "Lightgun_left = 1" line. The value of line 10 in the nuvee_ps2_usb_guncon1.ini can vary but it's always similar to "GUID = \\?\HID#VID_16C0&PID_0F38&MI_02&Col02#9&290b5653&0&0001#{378de44c-56ef-11d1-bc8c-00a0c91405dd}".

I want to modify the script so that it takes the value that it reads from line 10 of "nuvee_ps2_usb_guncon1.ini" at the beginning of the script and replaces the "GUID" line in all other nuvee_ps2_usb_guncon1.ini files whose path is specified. Below is what a typical full nuvee_ps2_usb_guncon1.ini files looks like. Would really appreciate any help, thanks!

Code: Select all

Sensitivity = 100.000000
Threshold = 512
Deadzone = 0
Left = 1
Right = 3
Middle = 5
Reload = 0
Calibration = 0
Cursor = 0
GUID = \\?\HID#VID_16C0&PID_0F38&MI_02&Col02#9&290b5653&0&0001#{378de44c-56ef-11d1-bc8c-00a0c91405dd}2
Lightgun_left = 1
Lightgun_top = 1
Lightgun_right = 65534
Lightgun_bottom = 65534
Model = 0
Alignment = 7
Aiming scale X = 100.000000
Aiming scale Y = 100.000000
Aiming profile = ninja_assault_e
Aux 1 = 17
Aux 2 = 17
Wheel up = 17
Wheel down = 17
Keyboard D-pad = 1
Start hotkey = 0
doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Re: Help with string replace

22 Jun 2021, 11:06

You could simplify it a bit with a Loop, Files
https://www.autohotkey.com/docs/commands/LoopFile.htm

it would look something like this. Note: I am not sure what your script is doing exactly and in what order so don't copy/paste and run this. Commented out for safety

Code: Select all

Loop Files, D:\TEST\*.ini, R ; look for all .ini files and recurse into subfolders
{
	;FileRead, gun1nuvee, % A_LoopFileFullPath
	;FileRecycle, % A_LoopFileFullPath
	;FileAppend, % StrReplace(gun1nuvee, prepend := "Lightgun_left = 1`r`n", gun1id "`n" prepend), % A_LoopFileFullPath
}
edit: To change text in an ini file that doesnt have sections (i.e. [Section Name] before any text) i think you are correct in running a string replace and replacing the file with the new text.
yardley
Posts: 30
Joined: 05 May 2021, 21:16

Re: Help with string replace

23 Jun 2021, 14:30

doubledave22 wrote:
22 Jun 2021, 11:06
You could simplify it a bit with a Loop, Files
https://www.autohotkey.com/docs/commands/LoopFile.htm

it would look something like this. Note: I am not sure what your script is doing exactly and in what order so don't copy/paste and run this. Commented out for safety

Code: Select all

Loop Files, D:\TEST\*.ini, R ; look for all .ini files and recurse into subfolders
{
	;FileRead, gun1nuvee, % A_LoopFileFullPath
	;FileRecycle, % A_LoopFileFullPath
	;FileAppend, % StrReplace(gun1nuvee, prepend := "Lightgun_left = 1`r`n", gun1id "`n" prepend), % A_LoopFileFullPath
}
edit: To change text in an ini file that doesnt have sections (i.e. [Section Name] before any text) i think you are correct in running a string replace and replacing the file with the new text.
Thank you, this definitely simplifies the code! Now I just need to figure out how to make the script replace the "GUID =" line without having to rely on prepending it to "Lightgun_left = 1".
doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Re: Help with string replace

23 Jun 2021, 18:12

Heres a test script you can play around with.

The regexmatch will match the full line containing GUID = xxxxx

The regexreplace can replace this full line with whatever text you put in Replacement_Text

Code: Select all


gun1nuvee = 
(join`n
Sensitivity = 100.000000
Threshold = 512
Deadzone = 0
Left = 1
Right = 3
Middle = 5
Reload = 0
Calibration = 0
Cursor = 0
GUID = \\?\HID#VID_16C0&PID_0F38&MI_02&Col02#9&290b5653&0&0001#{378de44c-56ef-11d1-bc8c-00a0c91405dd}2
Lightgun_left = 1
Lightgun_top = 1
Lightgun_right = 65534
Lightgun_bottom = 65534
Model = 0
Alignment = 7
Aiming scale X = 100.000000
Aiming scale Y = 100.000000
Aiming profile = ninja_assault_e
Aux 1 = 17
Aux 2 = 17
Wheel up = 17
Wheel down = 17
Keyboard D-pad = 1
Start hotkey = 0
)

RegExMatch(gun1nuvee, "s)GUID[^\v]*",Found_Line)

msgbox, % Found_Line

Replacement_Text := "Hello!"

Output := Regexreplace(gun1nuvee, "s)GUID[^\v]*",Replacement_Text)

msgbox, % Output

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: CuriousDad, rc76 and 231 guests