Use IfInString or RegEx to find matching text in file? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
bballjoe12
Posts: 114
Joined: 20 Aug 2017, 09:11

Use IfInString or RegEx to find matching text in file?

07 Feb 2018, 16:33

I have a script file, that part of the FileAppend process, creates a script file, Keyboard.ahk, using the variables, %FirstKey% and %SecondKey%.

Basically my notepad file(Keyboard.ahk) would look like this:

Code: Select all

!x::Send Text1
#s::Send Text2
^y::Text3
!, # and ^ are assigned to a variable, %FirstKey% based on some GUI options, and %SecondKey% contains the part after the '::' signs, based on some custom user options.

I also have a main GUI file that I want to check my Keyboard.ahk for exact matches of the strings above, so that there are not any duplicate hotkeys within the Keyboard.ahk. I've tried

Code: Select all

FileRead, CopyHK, Keyboard.ahk
IfInString, CopyHK, %FirstKey% %SecondKey%
	MsgBox, 0, Alert, You already have this HotKey Combination.
What am I doing wrong here?
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Use IfInString or RegEx to find matching text in file?  Topic is solved

07 Feb 2018, 17:01

Based on your description, I don't see where the key itself (x, s and y in the example above) appear in your variables. FirstKey contains just the modifiers and SecondKey contains the part after the "::"? Then your IfInString is missing the main key part as well as the "::".

It would be clearer if you showed exactly the range of values that FirstKey and SecondKey contain.

Your IfInString statement right now is looking for an exact match of whatever is in FirstKey followed by a space followed by whatever is in SecondKey. Not that the string contains those in various parts, but must contain that exact string of characters in a row.

You should either add the missing parts to your IfInString statement (and remove the extraneous space), or make it a compound statement like:
if (InStr(FirstKey) && InStr(SecondKey))
bballjoe12
Posts: 114
Joined: 20 Aug 2017, 09:11

Re: Use IfInString or RegEx to find matching text in file?

07 Feb 2018, 17:20

FirstKey can contain only ^, ! and # while Second Key can contain any letter a-z and or any number 0-9.

This is how FirstKey and SecondKey get appended to the Keyboard.ahk file:

Code: Select all

If FirstKeyCH = ALT	
{
	FileRead, CheckDHK, %A_MyDocuments%\Keyboard.ahk
	IfInString, CheckDHK, ShowHotkeyHelp
			{
			
			FileAppend,
			(
			!%SecondKeyCH%::Send %output%{Tab 3}
	 		
			), %A_MyDocuments%\Keyboard.ahk
	Gui, Main: Show				
	Gui, CH: Destroy
	WinMinimize, HotKey Creator
	MsgBox, 64, Attention, Please double click on the file, "Keyboard" after the window opens.
	Run, %A_MyDocuments%
	Sleep 100
		
Return	
			}
	else

			{
			FileAppend,
			(
		%DHK%`r
		!%SecondKeyCH%::Send %output%{Tab 3}`r
	 		
			), %A_MyDocuments%\Keyboard.ahk
			
	Gui, Main: Show				
	Gui, CH: Destroy
	WinMinimize, HotKey Creator
	MsgBox, 64, Attention, Please double click on the file, "Keyboard" after the window opens.
	Run, %A_MyDocuments%
	Sleep 100
	
Return
			}
}

	If FirstKeyCH = Windows Key	
{
	FileRead, CheckDHK, %A_MyDocuments%\Keyboard.ahk
	IfInString, CheckDHK, ShowHotkeyHelp
			{
			
			FileAppend,
			(
			#%SecondKeyCH%::Send %output%{Tab 3}
	 		
			), %A_MyDocuments%\Keyboard.ahk
					
	WinMinimize, Company/Hospital
	Sleep 100
	MsgBox, 64, Attention, Please double click on the file, "Keyboard" after the window opens.
	Run, %A_MyDocuments%
	Reload
Return	
			}
	else

			{
			FileAppend,
			(
			%DHK%`r
			#%SecondKeyCH%::Send %output%{Tab 3}`r
	 		
			), %A_MyDocuments%\Keyboard.ahk
			
	Gui, CH: Destroy
	;WinMinimize, Company/Hospital
	MsgBox, 64, Attention, Please double click on the file, "Keyboard" after the window opens.
	Run, %A_MyDocuments%
	Reload
Return
			}
}

	If FirstKeyCH = CTRL	
{
	FileRead, CheckDHK, %A_MyDocuments%\Keyboard.ahk
	IfInString, CheckDHK, ShowHotkeyHelp
			{
			
			FileAppend,
			(
		^%SecondKeyCH%::Send %output%{Tab 3}
	 		
			), %A_MyDocuments%\Keyboard.ahk
					
	WinMinimize, Company/Hospital
	Sleep 100
	MsgBox, 64, Attention, Please double click on the file, "Keyboard" after the window opens.
	Run, %A_MyDocuments%
	Reload
Return	
			}
	else

			{
			FileAppend,
			(
		%DHK%`r
		^%SecondKeyCH%::Send %output%{Tab 3}`r
	 		
			), %A_MyDocuments%\Keyboard.ahk
			
	WinMinimize, Company/Hospital
	Sleep 100
	MsgBox, 64, Attention, Please double click on the file, "Keyboard" after the window opens.
	Run, %A_MyDocuments%
	Reload
Return
			}
}

Return
I know I have A LOT of redundant code as I don't know how to not get the function I need without using it.
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Use IfInString or RegEx to find matching text in file?

07 Feb 2018, 17:44

Then remove the space between the two variables
bballjoe12
Posts: 114
Joined: 20 Aug 2017, 09:11

Re: Use IfInString or RegEx to find matching text in file?

07 Feb 2018, 20:26

No luck, unfortunately. Would there be another route I could take?
bballjoe12
Posts: 114
Joined: 20 Aug 2017, 09:11

Re: Use IfInString or RegEx to find matching text in file?

07 Feb 2018, 22:37

If I use the following code for checking for the Alt or Windows Key, it is basically checking to see if any HotKeys have the same letter (%SecondKey%) rather then checking for the whole combination.

Code: Select all

; Checks to see if any HotKeys already exist with the ALT key
IfInString, CopyHK, !%SecondKeyCH%
	{
	MsgBox, 48, Illegal Combination, Key combination already exists.  Combination must be changed.
	Return
	}

; Checks to see if any HotKeys already exist with the Windows key
IfInString, CopyHK, #%SecondKeyCH%
	{
	MsgBox, 48, Illegal Combination, Key combination already exists.  Combination must be changed.
	Return
	}
Basically, if the file already has !s to activate a HotKey, I couldn't use #s because it is finding the 's' as already existing. Not quite what I am needing..
Xeilous
Posts: 13
Joined: 04 Feb 2018, 04:06

Re: Use IfInString or RegEx to find matching text in file?

07 Feb 2018, 23:07

Does this help?

Code: Select all

FirstKey := "#"

SecondKey := "s"

Needle := FirstKey SecondKey

FileRead, OutputVar, Keyboard.ahk

If InStr(OutputVar, Needle)
	MsgBox, The string was found.
else
	MsgBox, The string was not found.

Code: Select all

;Keyboard.ahk
!x::Send Text1
#s::Send Text2
^y::Text3

Not sure if this is of any use to you, but the following code will check for all combinations.

Code: Select all

modifierKeys := ["!", "#", "^"]

secondKeys := ["x", "s", "y"]

FileRead, outputVar, Keyboard.ahk

for i, element in modifierKeys {
	for j, elementj in secondKeys {
		If InStr(outputVar, element elementj)
			MsgBox, % element elementj " was found."
	}
}
bballjoe12
Posts: 114
Joined: 20 Aug 2017, 09:11

Re: Use IfInString or RegEx to find matching text in file?

08 Feb 2018, 07:24

That last code you posted....

For some reason, it still detects my Keyboard.ahk as if I have a combination I don't really have there yet. I've tried a million different options with no luck :(

I even took the FileRead, Output, Keyboard.ahk and FileAppended it to a notepad file to just to see if Output had the correct information stored and it does!
bballjoe12
Posts: 114
Joined: 20 Aug 2017, 09:11

Re: Use IfInString or RegEx to find matching text in file?

08 Feb 2018, 08:17

Wow, I am an idiot. I was putting my code in the wrong spot... All is well now! THANK YOU EVERYONE!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, mikeyww and 201 guests