Multiple Variables In Message Box Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AlFlo
Posts: 345
Joined: 29 Nov 2021, 21:46

Re: Multiple Variables In Message Box

Post by AlFlo » 05 Aug 2022, 19:21

Hmmmm ... still a couple of stray results. Still testing.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Multiple Variables In Message Box

Post by BoBo » 06 Aug 2022, 04:55

Just out of curiosity, another approach:

Code: Select all

#SingleInstance, Force
SetWorkingDir, A_ScriptDir

;For example, if I had a file called "Joe Smith - Letter from Acme Hardware.pdf"
;I'd like to be able to enter "Smith" in the first space in the message box and "Acme" in the second space, 
;and have it find the file even those two words are not next to the other in the file path name.

InputBox, str, Directory filter, Enter the filter.,, 300, 125
p := StrSplit(str, A_Space)

Loop, Files,% A_ScriptDir "\*.pdf"						; change that path accordingly + the option to search subdirectories
   {  SplitPath, A_LoopFileName,,,, OutNameNoExt
      i:=0
      for key in p {
         InStr(OutNameNoExt, p[A_Index]) ? i++ : i
            if (i = p.Count()) {
               list .= A_LoopFileName "`n"
               i:=0
               }
         }
   }
MsgBox % RTrim(list,"`n")

AlFlo
Posts: 345
Joined: 29 Nov 2021, 21:46

Re: Multiple Variables In Message Box

Post by AlFlo » 06 Aug 2022, 12:53

My testing shows that both Mikeyww's second script and Bobo's script work 100% accurately when there are a lot of search words, but have errors in them when there are just one or two search words. The two scripts have the exact same errors when there are, for example, two search words.

I don't have the same errors when I use a script with multiple input boxes. And I believe that has the advantage of being able to "chunk" search terms. For example, if I type "red apples" in the first input box, "sale" in the second" and "August" in the third then it would find files and folder names where the words "red apples" are right next to each other, and "sale" and "August" also appear, but not files/folder names where "red" is separated from "apples". For example, it would find Red Apples Taste Good When They're On Sale in July or August.pdf, but not Red Is My Favorite Color and Apples Taste Good When They're On Sale in July or August.pdf.

Being able to chunk in this type of search is sort of like using quotation marks in a Google search: "red apples" sale august https://www.google.com/search?q=%22red+ ... e&ie=UTF-8

But since it seems impossible to set up multiple input areas in a single inputbox, the ideal still seems to have a child gui like in my not-working script I posted. But not sure...

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Multiple Variables In Message Box

Post by mikeyww » 06 Aug 2022, 13:11

My script found a file containing "red" and "apples". Will your problem examples & file list remain a secret?

AlFlo
Posts: 345
Joined: 29 Nov 2021, 21:46

Re: Multiple Variables In Message Box

Post by AlFlo » 06 Aug 2022, 13:15

Mikeyww, I'll send you my test results via private message. Thanks.

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Multiple Variables In Message Box

Post by mikeyww » 06 Aug 2022, 13:22

Sure, OK, or again, OK to post some dummy file names and examples of what input did not work for them.

AlFlo
Posts: 345
Joined: 29 Nov 2021, 21:46

Re: Multiple Variables In Message Box

Post by AlFlo » 06 Aug 2022, 13:57

For the sake of anyone reading this thread, Mikeyww explained to me through a private message that I was misunderstanding the search results, and that his second script works perfectly. D'oh! My mistake.

Thank you Mikeyww for writing a great script, and also for educating me about how I was confused.

Incidentally, I believe Bobo's script works also.

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Multiple Variables In Message Box  Topic is solved

Post by mikeyww » 06 Aug 2022, 14:05

No trouble. This one is actually adapted from BoBo, for words.

Code: Select all

pathList := [StrReplace(A_Desktop, "Desktop", "Documents"), A_ScriptDir]
InputBox, str, Word filter, Enter the filter.,, 300, 125
If (ErrorLevel || str = "")
 Return
p := StrSplit(str, " ")
For each, filePath in pathList
 Loop, Files, %filePath%\*, FDR
 { SplitPath, A_LoopFileName,,,, fnBare
   i = 0
   For each, word in p
    i += RegExMatch(fnBare, "i)\b\Q" word "\E\b") > 0 ; Matches words rather than any text
   list .= i = p.Count() ? A_LoopFilePath "`n" : ""
 }
MsgBox, 64, Matches, % Trim(list, "`n")

AlFlo
Posts: 345
Joined: 29 Nov 2021, 21:46

Re: Multiple Variables In Message Box

Post by AlFlo » 06 Aug 2022, 14:28

Works like a charm. Thank you for all of your help!

AlFlo
Posts: 345
Joined: 29 Nov 2021, 21:46

Re: Multiple Variables In Message Box

Post by AlFlo » 12 Aug 2022, 11:50

Mikeyww, I'm sorry but I'm having trouble changing the message box at the end of your script into a list in a gui, so I can display the results in a list. Here's what I'm trying:

Code: Select all

Gui, Add, ListBox, vMyList x16 y24 w1500 h1000 ; creates the basic gui

Gui, Show,, Test

Gui Font

pathList := ["C:\Users\PATH\OneDrive\Documents\"] ; this is the start of your excellent script

InputBox, filter, Directory filter, Enter the filter.,, 300, 125
If (ErrorLevel || filter = "")
 Return
wordList := StrSplit(filter, " ")
For each, filePath in pathList
 Loop, Files, %filePath%\*, FDR
 { include := True
   For every, word in wordList
    If !Instr(A_LoopFileName, word) {
     include := False
     Break
    }
   list .= include ? (list > "" ? "`n" : "") A_LoopFilePath : ""
 }

GuiControl,, MyList, %list%  ; Here's where I'm trying to list the results in my GUI
Gui, Show
Return
This isn't working. What am I doing wrong?

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Multiple Variables In Message Box

Post by mikeyww » 12 Aug 2022, 11:58

An example is below.

Code: Select all

pathList := [StrReplace(A_Desktop, "Desktop", "Documents"), A_ScriptDir]
InputBox, str, Word filter, Enter the filter.,, 300, 125
If (ErrorLevel || str = "")
 Return
p := StrSplit(str, " ")
For each, filePath in pathList
 Loop, Files, %filePath%\*, FDR
 { SplitPath, A_LoopFileName,,,, fnBare
   i = 0
   For each, word in p
    i += RegExMatch(fnBare, "i)\b\Q" word "\E\b") > 0 ; Matches words rather than any text
   list .= i = p.Count() ? A_LoopFilePath "|" : ""
 }
Gui, Font, s10
Gui, Add, ListBox, w700 r10, % Trim(list, "|")
Gui, Show,, Matches
Explained: ListBox
The last parameter is a pipe-delimited list of choices such as Choice1|Choice2|Choice3.

AlFlo
Posts: 345
Joined: 29 Nov 2021, 21:46

Re: Multiple Variables In Message Box

Post by AlFlo » 15 Aug 2022, 17:59

Mikeyww,

Your script works like a charm to list the results in my GUI. But for some reason, I can't get it to take actions on the selected list result.

For example, one of my buttons is supposed to open the selected file or folder from the list, but it is instead just opening my MyDocs folder. Here's my script so far:

Code: Select all

pathList := ["C:\Users\path\OneDrive\Documents\"]

Gui, Add, ListBox, vMyList x16 y24 w1500 h1000 ; creates the basic gui

Gui, Show,, Test

Gui Font

Gui Add, Button, x1 y1 w75 h23 gOpenFolder, Open

InputBox, str, Word filter, Enter the filter.,, 300, 125
If (ErrorLevel || str = "")
 Return
p := StrSplit(str, " ")
For each, filePath in pathList
 Loop, Files, %filePath%\*, FDR
 { SplitPath, A_LoopFileName,,,, fnBare
   i = 0
   For each, word in p
    i += RegExMatch(fnBare, "i)\b\Q" word "\E\b") > 0 ; Matches words rather than any text
   list .= i = p.Count() ? A_LoopFilePath "|" : ""
 }
Gui, Font, s10
Gui, Add, ListBox, w700 r10, % Trim(list, "|")
Gui, Show,, Matches

GuiControl,, MyList, %list%
Gui, Show
Return

OpenFolder:
GuiControlGet, Selection,, MyList
Run, Explorer "%pathList%%Selection%"
Gui , Hide
Return
When I use a MsgBox to display %pathList%%Selection%" it shows the right filepath for the selected file or folder. But for some reason it won't open it through Explorer.

What am I doing wrong?

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Multiple Variables In Message Box

Post by mikeyww » 16 Aug 2022, 07:54

You can debug your script by displaying values of variables & functions. That makes the process fast and easy, and it requires no additional knowledge about AutoHotkey. In your script, pathList is an array, not a text string. Your ListBox includes the full path as shown, so you would not add a path to it.

Code: Select all

filePath := StrReplace(A_Desktop, "Desktop", "Documents")
InputBox, str, Word filter, Enter the filter.,, 300, 125
If (ErrorLevel || str = "")
 Return
Loop, Files, %filePath%\*, FDR
{ SplitPath, A_LoopFileName,,,, fnBare
  i = 0
  For each, word in p := StrSplit(str, " ")
   i += RegExMatch(fnBare, "i)\b\Q" word "\E\b") > 0 ; Matches words rather than any text
  list .= i = p.Count() ? A_LoopFilePath "|" : ""
}
Gui, Font, s10
Gui, Add, ListBox, w700 r30 vsel, %list%
Gui, Add, Button , wp   Default , Open
Gui, Show,, Matches
Return

ButtonOpen:
Gui, Submit
Run, %sel%
Return

GuiEscape:
GuiClose:
ExitApp

AlFlo
Posts: 345
Joined: 29 Nov 2021, 21:46

Re: Multiple Variables In Message Box

Post by AlFlo » 16 Aug 2022, 14:42

Thank you, Mikeyww ... I greatly appreciate your help!

AlFlo
Posts: 345
Joined: 29 Nov 2021, 21:46

Re: Multiple Variables In Message Box

Post by AlFlo » 21 Sep 2022, 17:57

Bobo, Mikeyww and everyone,

After tinkering around for a couple of months, I've finally used your scripts to get search results of file and folder names (1) for search input of partial words and (2) for full words.

Is there any way to highlight the search strings when searching for partial words, the way that the Everything app does? To demonstrate what I mean, here's a screenshot of results from my AHK script (where nothing is bolded related to the search string "tes" in the results list, so not as useful):
AHK Screenshot.jpg
AHK Screenshot.jpg (46.55 KiB) Viewed 624 times
And here's a screenshot from a similar results from Everything (where the search string "tes" is bolded within the results list, so I can quickly locate the search string within the results to see if it's what I'm looking for
Everything Screenshot.jpg
Everything Screenshot.jpg (15.88 KiB) Viewed 624 times
).

Thanks!

By the way, the code I'm using (ignoring all of the buttons and subroutines) is as follows:

Code: Select all

BasePath1 := "C:\Users\myname\DOWNLOADS\"
BasePath2 := "C:\Users\myname\OneDRIVE\Documents\" 
InputBox, str, Word filter, Enter the filter.,, 300, 125
p := StrSplit(str, A_Space)

loop, Files, % BasePath1 "*.*", FDR
 {  SplitPath, A_LoopFileName,,,, OutNameNoExt
      i:=0
      for key in p {
         InStr(OutNameNoExt, p[A_Index]) ? i++ : i
            if (i = p.Count()) {
 ;              list .= A_LoopFileName "`n"
    list .= i = p.Count() ? A_LoopFilePath "|" : ""
             i:=0
               }
         }
   }

loop, Files, % BasePath2 "*.*", FDR
 {  SplitPath, A_LoopFileName,,,, OutNameNoExt
      i:=0
      for key in p {
         InStr(OutNameNoExt, p[A_Index]) ? i++ : i
            if (i = p.Count()) {
;               list .= A_LoopFileName "`n"
  list .= i = p.Count() ? A_LoopFilePath "|" : ""
               i:=0
               }
         }
   }
Gui, Add, ListBox, x16 y24 w1500 h1000 vListBox_ClList, %list%

Gui, Show,, Test

Gui Font

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Multiple Variables In Message Box

Post by mikeyww » 22 Sep 2022, 04:55

You could probably do it with HTML in an ActiveX control. BoBo may have some better ideas.

Post Reply

Return to “Ask for Help (v1)”