Simple text search from text-file?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Simple text search from text-file?

06 Apr 2017, 10:15

I think that's this is nearly a standard solution, so maybe there exists some plug&play script?

- Read a predefined text-file
- search all strings containing *mysearch*
- list them to user
- user clicks to input/paste it

If a code like this exists I would be happy for a hint/link

Peter
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Simple text search from text-file?

06 Apr 2017, 10:34

Untested!

Code: Select all

FilePath := "complete path to the string-containing file goes here"

^a::
	InputBox, SearchString, , % "Input string to search."
	ResultList := []
	ChosenString := ""
	Loop, Read, %FilePath%
		if (InStr(A_LoopReadLine, SearchString))
			ResultList.Push(A_LoopReadLine)
	if (!ResultList.Length)
	{
		MsgBox % "No match found!"
		return
	}
	Gui, DisplayResults:New
	Gui, Add, Text, , % "Click on a line to select it."
	for key, value in ResultList
	{
		if (key = 1)
			Gui, Add, Radio, gSelectResult vChosenString, %value%
		else
			Gui, Add, Radio, gSelectResult, %value%
	Gui, DisplayResults:Show
	return
	
SelectResult:
	Gui, DisplayResults:Submit
	Send % ResultList[ChosenString]
	return
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Re: Simple text search from text-file?

06 Apr 2017, 12:15

Great-thanks. Here is the tested version.

Code: Select all

    FilePath := "complete path to the string-containing file goes here"
^a::
    InputBox, SearchString, , % "Input string to search."
    ResultList := []
    ChosenString := ""
    Loop, Read, %FilePath%
    {
        if (InStr(A_LoopReadLine, SearchString))
        {
            ResultList.Push(A_LoopReadLine)
        }
    }
    if (!ResultList.Length())
    {
        MsgBox % "No match found!"
        return
    }
    Gui, DisplayResults:New,-MaximizeBox -MinimizeBox  , Search results
    Gui, Add, Text, w250, % "Click on a line to select and type it."
    for key, value in ResultList
    {
        if (key = 1)
            Gui, Add, Radio, gSelectResult vChosenString, %value%
        else
            Gui, Add, Radio, gSelectResult, %value%
    }
    Gui, DisplayResults:Show
    return
    
SelectResult:
    Gui, DisplayResults:Submit
    Send % ResultList[ChosenString]
    return
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Simple text search from text-file?

06 Apr 2017, 12:19

Glad that worked for you, it was pretty much off the top of my head. :)
Peter2
Posts: 325
Joined: 21 Sep 2014, 14:38
Location: CH

Re: Simple text search from text-file?

06 Apr 2017, 12:35

MaxAstro wrote:... off the top of my head. :)
There must be a lot of AHK in it ... :bravo:
Peter (AHK Beginner) / Win 10 x64, AHK Version v1.1.33
glogin
Posts: 1
Joined: 18 Nov 2015, 09:11

Re: Simple text search from text-file?

21 Sep 2021, 07:25

What would the script look like if they referred to the text string that would be in the script?

Thank you!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: wilkster and 286 guests