Multiple Variables In Message Box Topic is solved

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

Multiple Variables In Message Box

Post by AlFlo » 04 Aug 2022, 19:07

FlyingDman helped me create a powerful script to find files and folders with a search string in it (code is below). Running the script:

(1) Opens a message box, in which I type the string I want to search for; then
(2) Quickly shows a list of results.

But this only works if the characters in the string are sequential. I want to be able to narrow the search results by entering two different strings through a message box with two space to enter the two different strings.

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.

I wrote the script below which works by popping up 2 separate message boxes. But is there a way to revise the script so that it pops up a single message box which allows entry of two strings?

Code: Select all

#NoEnv
SetBatchLines, -1  ; affects CPU utilization... script will run at max speed
ListLines Off  ; helps with speed
#SingleInstance Force

Gui, Add, ListBox, vListBox_ClList x16 y24 w1500 h1000

Gui, Show,, Test

Gui Font

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

BasePath1 := "C:\Users\PATH\DOWNLOADS\"
BasePath2 := "C:\Users\PATH\OneDRIVE\Documents\" 

InputBox, Filter, Directory Filter, Filter?:,, 240, 130, 800, 1
InputBox, Filter1, Directory Filter, Filter1?:,, 240, 130, 800, 1

loop, Files, % BasePath "*.*", FDR
	if InStr(A_LoopFileFullPath, Filter) & InStr(A_LoopFileFullPath, Filter1)

		FileList .= A_LoopFileFullPath "|"
loop, Files, % BasePath2 "*.*", FDR
	if InStr(A_LoopFileFullPath, Filter)
		FileList .= A_LoopFileFullPath "|"

FileList := RTrim(FileList, "|")
Sort, FileList, D|
FileList := StrReplace(FileList, BasePath)
GuiControl,, ListBox_ClList, %FileList% 
Gui, Show
return

OpenSesame:

GuiControlGet, Selection,, ListBox_ClList
Run, Explorer "%BasePath%%Selection%"
 
Gui , Hide
Return

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

Re: Multiple Variables In Message Box

Post by mikeyww » 04 Aug 2022, 19:11

Before getting into it, your confirmation that you want this would be useful, because Everything is a free program that already does it. It can instantly show you the list of every matching file on your computer.

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

Re: Multiple Variables In Message Box

Post by AlFlo » 04 Aug 2022, 19:15

mikeyww wrote:
04 Aug 2022, 19:11
Before getting into it, your confirmation that you want this would be useful, because Everything is a free program that already does it. It can instantly show you the list of every matching file on your computer.
Thanks, Mikeyww. I already have FileLocator Pro, which I assume is like Everything. Where this Autohotkey script is useful is that it can immediately locate folders. And I've actually expanded the script with additional buttons to be able to instantly:

(1) Open the selected folder (or file);
(2) Show the folder (or file's) location in the file tree through Windows Explorer;
(3) Create a new subfolder within the specified folder;
(4) Save my open Word doc within the specified folder;
(5) Save my open .pdf file within the specified folder;
(6) Save my open Outlook message within the specified folder; and
(7) Extract all yellow highlighted text in a specified Word doc into a new doc.

So it's useful for me.

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

Re: Multiple Variables In Message Box

Post by mikeyww » 04 Aug 2022, 19:24

Code: Select all

pathList := [StrReplace(A_Desktop, "Desktop", "Downloads"), A_ScriptDir]
InputBox, filter, Directory filter, Enter the filter.,, 300, 125
If (ErrorLevel || filter = "")
 Return
word := StrSplit(filter, " "), maskList := [], a := "*"
maskList.Push(a word.1 a word.2 a), maskList.Push(a word.2 a word.1 a)
For each, filePath in pathList
 For every, mask in maskList
  Loop, Files, %filePath%\%mask%, FDR
   list .= (list > "" ? "`n" : "") A_LoopFilePath
MsgBox, 64, List, %list%
Return
Or:

Code: Select all

pathList := [StrReplace(A_Desktop, "Desktop", "Downloads"), A_ScriptDir]
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 : ""
 }
MsgBox, 64, List, %list%
Return

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

Re: Multiple Variables In Message Box

Post by AlFlo » 04 Aug 2022, 20:50

Not working for me. I've tried both versions of your script, and both times don't get any list, just the following result:
List Error Screenshot.png
List Error Screenshot.png (2.51 KiB) Viewed 2025 times

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

Re: Multiple Variables In Message Box

Post by mikeyww » 04 Aug 2022, 21:02

It worked here-- a demonstration-- but if the specific paths in this script do not match the paths that you intend to use, then not much will happen. You can change the path list to match what you need.

One way to debug a script is to add a few MsgBox lines that show the values of your variables, functions, & conditional statements. You can then understand where the script is going wrong. You could show what paths are being checked, what files are being found, what word is being checked, and what Instr value is returned.

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

Re: Multiple Variables In Message Box

Post by AlFlo » 05 Aug 2022, 16:50

Mikeyww, through trial and error, I found that both of your scripts work when there is one word entered as the string in the inputbox. But they fail if there are multiple words.

By way of example, my old script would work if I type "Low Cost" into the input box in finding all file or folder names with the phrase "low cost" in them, but it would not search for other strings not immediately adjacent to that phrase. Your scripts work with "low" or with "cost", but not with "low cost".

I'm playing around with a child GUI which would work perfectly for my purpose ... but I just can't get the "Done" button to close the child GUI and run the strings which I entered in the child gui as variables in my main script.

I've stumbled into quite a few forum threads where you show people how to use parent and child guis, but I'm stuck.

Here's what I'm trying:

Code: Select all

#NoEnv
SetBatchLines, -1  ; affects CPU utilization... script will run at max speed
ListLines Off  ; helps with speed
#SingleInstance Force

Gui, Add, ListBox, v1 x16 y24 w1500 h1000

Gui, Show,, Test

Gui Font

; I have a bunch of buttons here for my main GUI.


BasePath := "C:\Users\PATH\OneDRIVE\Documents\"
FileList := ""

Gui,2:+Owner1
Gui,2:show,w1800 h250


Gui,2: Add, Text, xm w40, Term 1:
Gui,2: Add, Edit, x+m w1700 vFilter
Gui,2: Add, Text, xm w40, Term 2:
Gui,2: Add, Edit, x+m w1700 vFilter1
Gui,2: Add, Text, xm w40, Term 3:
Gui,2: Add, Edit, x+m w1700 vFilter2
Gui,2: Add, Text, xm w40, Term 4:
Gui,2: Add, Edit, x+m w1700 vFilter3
Gui,2: Add, Text, xm w40, Term 5
Gui,2: Add, Edit, x+m w1700 vFilter4
Gui,2: Add, Button, xm w100, Done
Gui,2: Show,, Questionaire

Return

2GuiEscape:
2GuiClose:
ExitApp

ButtonDone:
    Gui,2: Submit, Hide

loop, Files, % BasePath "*.*", FDR
	if InStr(A_LoopFileFullPath, Filter) & InStr(A_LoopFileFullPath, Filter1) & InStr(A_LoopFileFullPath, Filter2) & InStr(A_LoopFileFullPath, Filter3) & InStr(A_LoopFileFullPath, Filter4)
		FileList .= A_LoopFileFullPath "|"
FileList := RTrim(FileList, "|")
Sort, FileList, D|
FileList := StrReplace(FileList, BasePath)
GuiControl,, ListBox_ClList, %FileList% 
Gui,1: Show
return

; I've got a bunch of things here that the buttons in my main GUI make happen

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

Re: Multiple Variables In Message Box

Post by mikeyww » 05 Aug 2022, 18:17

Strange. My second script worked here. Which files do you have in the path list that are supposed to match? Which directories contain those files? Have you adjusted the path list as I mentioned? If so, what is it?
Last edited by mikeyww on 05 Aug 2022, 18:19, edited 1 time in total.

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

Re: Multiple Variables In Message Box

Post by AlFlo » 05 Aug 2022, 18:19

I'm trying not to give actual client names is they have confidential client info in them. Can I shoot you a private message?

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

Re: Multiple Variables In Message Box

Post by mikeyww » 05 Aug 2022, 18:20

Certainly, or just make up some new dummy files, and test those in your script's directory. Either way.

image220805-1922-002.png
Input
image220805-1922-002.png (11.4 KiB) Viewed 1757 times
image220805-1925-001.png
Output
image220805-1925-001.png (7.99 KiB) Viewed 1755 times
Remember to adjust the paths as I mentioned. If needed, you can post your revised script below.
Last edited by mikeyww on 05 Aug 2022, 18:25, edited 1 time in total.

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

Re: Multiple Variables In Message Box

Post by AlFlo » 05 Aug 2022, 18:25

I created the following dummy files in MyDocs folder:

Test for Ahk 1.docx
Test for Ahk 2.docx
Test for Ahk Dude.docx

When I run your first script and enter test as the string in the input box, I get many results in my message box (I have a LOT of files with the word "Test" in them).

But when I type test for or test for AHK, I get the same error message I posted earlier with no results.

By the way, I'm using AHK Version 1.1.34.01. Are you using version 2?
Last edited by AlFlo on 05 Aug 2022, 18:26, edited 1 time in total.

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

Re: Multiple Variables In Message Box

Post by mikeyww » 05 Aug 2022, 18:26

Post the script that you are using below. As I mentioned twice above, you need to adjust the path list in the script. 1.1.34.03.

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

Re: Multiple Variables In Message Box

Post by AlFlo » 05 Aug 2022, 18:27

I'm using your first script verbatim:

Code: Select all

#NoEnv
SetBatchLines, -1  ; affects CPU utilization... script will run at max speed
ListLines Off  ; helps with speed
#SingleInstance Force

pathList := [StrReplace(A_Desktop, "Desktop", "Downloads"), A_ScriptDir]
InputBox, filter, Directory filter, Enter the filter.,, 300, 125
If (ErrorLevel || filter = "")

Return

word := StrSplit(filter, " "), maskList := [], a := "*"
maskList.Push(a word.1 a word.2 a), maskList.Push(a word.2 a word.1 a)

For each, filePath in pathList
 For every, mask in maskList
  Loop, Files, %filePath%\%mask%, FDR

   list .= (list > "" ? "`n" : "") A_LoopFilePath

MsgBox, 64, List, %list%
Return
I'm using AHK Version 1.1.34.01. Are you using version 2?
Last edited by AlFlo on 05 Aug 2022, 18:28, edited 1 time in total.

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

Re: Multiple Variables In Message Box

Post by mikeyww » 05 Aug 2022, 18:28

I guess I will say a fourth time then: adjust the path list to match your paths. If you need assistance, holler.

If you change the script, it is not my script verbatim. A different script may yield different results.

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

Re: Multiple Variables In Message Box

Post by AlFlo » 05 Aug 2022, 18:30

Yes, but it's exactly the same when I use:

Code: Select all

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

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

Re: Multiple Variables In Message Box

Post by mikeyww » 05 Aug 2022, 18:31

You are mistaken. An array differs from a non-array. Retain the brackets as shown.

I did not use your initial lines. Although they may have no adverse effect, it's best not to equate scripts that actually differ. Verbatim is "word for word".

These practices will speed the debugging process!

You can post your revised script below.

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

Re: Multiple Variables In Message Box

Post by AlFlo » 05 Aug 2022, 18:41

Thanks ... the brackets made it work. I can now enter multiple words.

But I believe I'm getting some incorrect results mixed in with the correct ones. Let me investigate further.

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

Re: Multiple Variables In Message Box

Post by AlFlo » 05 Aug 2022, 19:04

I set up a test folder so I could show you errors without publicly posting client info ...

First, here's the script I used:

Code: Select all

pathList := ["C:\Users\path\OneDrive\Documents\My Test Folder\"]
InputBox, filter, Directory filter, Enter the filter.,, 300, 125
If (ErrorLevel || filter = "")
 Return
word := StrSplit(filter, " "), maskList := [], a := "*"
maskList.Push(a word.1 a word.2 a), maskList.Push(a word.2 a word.1 a)
For each, filePath in pathList
 For every, mask in maskList
  Loop, Files, %filePath%\%mask%, FDR
   list .= (list > "" ? "`n" : "") A_LoopFilePath
MsgBox, 64, List, %list%
Return
I typed in the input box:

test for AHK

Here are the results (crossing out my name since posting publicly):
List.png
List.png (15.03 KiB) Viewed 1602 times
I believe it includes results which do not include the string "AHK"

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

Re: Multiple Variables In Message Box

Post by mikeyww » 05 Aug 2022, 19:09

Use the second script that I posted. That is what I meant when I wrote, "Strange. My second script worked here."

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

Re: Multiple Variables In Message Box

Post by AlFlo » 05 Aug 2022, 19:18

I think the second one is working!!!!!!!!!!!!! Let me test it some more.

Post Reply

Return to “Ask for Help (v1)”