search for a subfolder from its grandparent folder Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
JKnight_xbt33
Posts: 135
Joined: 18 Sep 2019, 02:06

search for a subfolder from its grandparent folder

Post by JKnight_xbt33 » 09 Jun 2021, 04:56

Dear all,
I have a relatively simple problem, still haven't found the solution.

Just looking for a way to search for a subfolder without having to hard code a new line in my script for its parent folder (e.g. 1301 - 1400)

For example if i wanted to search for a folder called 1699 I would have to add a new line in my script for the parent folder 1601 - 1700

I would much rather be able to search all the subfolders from the grandparent folder: \\private\records\

Code: Select all

F3::

Sleep, 10

InputBox, folder, Enter Folder

IfExist, \\private\records\1301 - 1400\%folder%
Run \\private\records\1301 - 1400\%folder%

IfExist, \\private\records\1401 - 1500\%folder%
Run \\private\records\1401 - 1500\%folder%

return
appreciated
J

User avatar
boiler
Posts: 8835
Joined: 21 Dec 2014, 02:44

Re: search for a subfolder from its grandparent folder

Post by boiler » 09 Jun 2021, 07:13

Make use of recursive folder search:

Code: Select all

F3::
InputBox, Folder, Enter Folder
Path := ""
loop, Files, \\private\records\%Folder%, DR
	Path := A_LoopFileFullPath
if Path
	Run, % Path
else
	MsgBox, % "Folder '" Folder "' was not found"
return

JKnight_xbt33
Posts: 135
Joined: 18 Sep 2019, 02:06

Re: search for a subfolder from its grandparent folder

Post by JKnight_xbt33 » 09 Jun 2021, 10:15

thanks @boiler
the script works its just that it takes a while to load the folder (~10 seconds)

Is there a way to speed up the recursive search?

Think I may have to accept the answer but not sure if the speed sacrifice is worth it.

best
J

User avatar
boiler
Posts: 8835
Joined: 21 Dec 2014, 02:44

Re: search for a subfolder from its grandparent folder

Post by boiler » 09 Jun 2021, 12:12

This version should speed it up some, especially if the folder is found earlier in the search. One thing that will help is if the folders where it might be found appear first, ahead of other folders you may have in the grandparent folder, in the way they are sorted by Windows.

Code: Select all

F3::
InputBox, Folder, Enter Folder
Path := ""
loop, Files, \\private\records\%Folder%, DR
{
	Path := A_LoopFileFullPath
	break
}
if Path
	Run, % Path
else
	MsgBox, % "Folder '" Folder "' was not found"
return

JKnight_xbt33
Posts: 135
Joined: 18 Sep 2019, 02:06

Re: search for a subfolder from its grandparent folder

Post by JKnight_xbt33 » 11 Jun 2021, 03:29

Thanks @boiler ,
I've tried your suggested script and it still seems quite slow.

I'll accept the answer because it works in principle but if anyone ever looks back on this post they need to be aware of the speed limitation.

best
J

User avatar
boiler
Posts: 8835
Joined: 21 Dec 2014, 02:44

Re: search for a subfolder from its grandparent folder

Post by boiler » 11 Jun 2021, 06:40

The general recursion approach has it checking within all of the folders at the grandchild level unnecessarily, and even at lower levels if there are folders inside of them. A faster approach then would be for it to have a nested loop where the outer loop identifies all folders at the parent level, then the inner loop would check only one level inside them, instead of needlessly going inside of each folder at the grandchild level. You might want to try that and post if you need help.

JKnight_xbt33
Posts: 135
Joined: 18 Sep 2019, 02:06

Re: search for a subfolder from its grandparent folder

Post by JKnight_xbt33 » 14 Jun 2021, 06:07

thanks for the advice,

I just made my own nested loop using your advice but its still slow. Any tips on what can be improved from below?
The first loop has the %folder% variable in place of the parent folder whereas the 2nd loop has it in place of the subfolder.

I wasn't sure how to specify looking one level inside the parent folder so the 2nd loop may be incorrect.

Code: Select all

F3::
InputBox, Folder, Enter Folder
Path := ""
loop, Files, \\private\%Folder%, DR
{
	Path := A_LoopFileFullPath
	if Path
	Run, % Path
	Else
	MsgBox Folder %folder% not found!

{loop, Files, \\private\records\%Folder%, DR
{
	Path := A_LoopFileFullPath
	if Path
	Run, % Path
	Else
	MsgBox Folder %folder% not found!
}
}}
return

Appreciate all your advice so far
J

User avatar
boiler
Posts: 8835
Joined: 21 Dec 2014, 02:44

Re: search for a subfolder from its grandparent folder

Post by boiler » 14 Jun 2021, 08:16

It's good that you made an attempt, but there are several issues with that code, which I'll list here in case it helps in learning some concepts:
  • You still are using the recursion option (R) in your file loops, so that wouldn't help reduce the time at all.
  • I don't see any reason to loop at the "private" folder level since I believe the parent folders (the ones with the range of numbers) only appear at the "private\records" level.
  • You are searching for the ultimate folder (the one entered by the user) at each level. The idea is to loop through the folders at one level (the "private\records" level), then look inside each of the folders found there in an inner loop for the ultimate folder, since that is where it would be found.
  • You are treating each level of looping as if it would find the folder and then run it (open it in File Explorer) from there. The intended approach is that the outer loop only provides a folder within which the inner loop looks for the ultimate folder and runs it. That is typically the idea of nested loops in general. The two loops cycle through two levels of groups of items that are ultimately only acted on inside that inner level.
  • Your inner loop would report that the file is not found inside each folder that it is not found. That should only be reported if it made it through the entire set of nested loops without finding it.
Overall, it makes the code much simpler:

Code: Select all

F3::
	InputBox, Folder, Enter Folder
	loop, Files, \\private\records\*, D
	{
		loop, Files, %A_LoopFileFullPath%\*, D
		{
			if (A_LoopFileName = Folder) {
				Run, % A_LoopFileFullPath ; only gets here if it's the actual desired folder
				return ; stop looking and return
			}
		}
	}
	MsgBox Folder %folder% not found! ; only gets here if the path was never found above
return
But upon further consideration, we don't even need an inner loop since we know we are at the right level to look directly for the ultimate folder name, so we can just check for it existing at that level, which would make it even simpler and faster:

Code: Select all

F3::
	InputBox, Folder, Enter Folder
	loop, Files, \\private\records\*, D
	{
		if InStr(FileExist(A_LoopFileFullPath "\" Folder), "D") {
			Run, % A_LoopFileFullPath "\" Folder
			return ; stop looking and return
		}
	}
	MsgBox Folder %folder% not found! ; only gets here if the path was never found above
return

JKnight_xbt33
Posts: 135
Joined: 18 Sep 2019, 02:06

Re: search for a subfolder from its grandparent folder

Post by JKnight_xbt33 » 23 Jun 2021, 05:24

Hi @boiler ,
thanks for the scripts. I tried the second one with inner loop removed, it came up with the message saying folder is not found.

I've decided to post the full file path of the real folders I use (for troubleshooting purposes) but I've anonimised the data.
I attempted to find the folder 2201 which i verified exists

I have a feeling there is something subtle/obvious which I am missing, maybe a messagebox verification is needed but I am not sure what it is.
What are your thoughts on this?

Appreciated
J

filepath of folder I am searching for:

Code: Select all

\\xxxx\shared\Departments\xxxx\xxxx\Team\xxxx\yyyy\zzzz\2201 - 2300\2201
attempt 1 but searching from grandparent folder

Code: Select all

F3::
	InputBox, Folder, Enter Folder
	loop, Files, \\xxxx\shared\Departments\xxxx\xxxx\Team\xxxx\yyyy\zzzz\*, D   ;filepath is anonimised but this is grandparent folder
	{
		if InStr(FileExist(A_LoopFileFullPath "\" Folder), "D") {
			Run, % A_LoopFileFullPath "\" Folder
			return ; stop looking and return
		}
	}
	MsgBox Folder %folder% not found! ; only gets here if the path was never found above
return
attempt 2 but searching from parent folder

Code: Select all

F3::
	InputBox, Folder, Enter Folder
	loop, Files, \\xxxx\shared\Departments\xxxx\xxxx\Team\xxxx\yyyy\zzzz\2201 - 2300\*, D  ;filepath is anonimised but this is the parent folder
	{
		if InStr(FileExist(A_LoopFileFullPath "\" Folder), "D") 
		{
			Run, % A_LoopFileFullPath "\" Folder
			return ; stop looking and return
		}
	}
	MsgBox Folder %folder% not found! ; only gets here if the path was never found above
return

User avatar
boiler
Posts: 8835
Joined: 21 Dec 2014, 02:44

Re: search for a subfolder from its grandparent folder  Topic is solved

Post by boiler » 23 Jun 2021, 07:00

Using the parent folder would not work. It is set up to cycle through parent folders from the grandparent folder. Are you sure the name of your folder is 2201 and nothing else? No trailing spaces or something?

I tested it here and it works. In the folder where the script resides, I have a folder named private, and inside that I have one named records, and inside that folder, I have a folder named 1601 - 1700, and inside that I have one named 1650. When I run the code below and input 1650, it opens a File Explorer window to the 1650 directory. If I enter something else, it tells me it's not found.

Code: Select all

F3::
	InputBox, Folder, Enter Folder
	loop, Files, private\records\*, D
	{
		loop, Files, %A_LoopFileFullPath%\*, D
		{
			if (A_LoopFileName = Folder) {
				Run, % A_LoopFileFullPath ; only gets here if it's the actual desired folder
				return ; stop looking and return
			}
		}
	}
	MsgBox Folder %folder% not found! ; only gets here if the path was never found above
return

Make sure that your path is correct and actually cycling through parent folders by running this version that will show a MsgBox for each parent folder it finds:

Code: Select all

F3::
	InputBox, Folder, Enter Folder
	loop, Files, \\xxxx\shared\Departments\xxxx\xxxx\Team\xxxx\yyyy\zzzz\*, D   ;filepath is anonimised but this is grandparent folder
	{
		MsgBox, % "Checking parent folder:`n`n" A_LoopFileFullPath
		if InStr(FileExist(A_LoopFileFullPath "\" Folder), "D") {
			Run, % A_LoopFileFullPath "\" Folder
			return ; stop looking and return
		}
	}
	MsgBox Folder %folder% not found! ; only gets here if the path was never found above
return
When running this version, does it show any MsgBox messages with "Checking parent folder: ..."? Does it show the one that contains the folder you asked it to find?

JKnight_xbt33
Posts: 135
Joined: 18 Sep 2019, 02:06

Re: search for a subfolder from its grandparent folder

Post by JKnight_xbt33 » 24 Jun 2021, 05:06

@boiler,
Perfect it works and looks through each folder as indicated with the messagebox.
I removed the messagebox and it also works. :D

I think I know what the issue was at first. In the actual script I had the hotkey F9 instead of F3 because I already use F3 for another script in my day to day work.
After hitting F9 the message box didnt say "enter folder" but said "enter request" which i thought was weird.

So I disabled all my autohotkey scripts and changed F9 to F3 and now for some reason it worked perfectly.

I checked all my scripts and stupidly didn't realise I had an old script running with the F9 hotkey already.

Sorry for this wild goose chase but at least its working and this will definately be a more effecient method to search folders rather than typing each new parent folder in the next iteration of the script.

Next time I will double check hotkeys are not already duplicated.

Very much appreciate your wisdom and knowledge on this Boiler
Thanks
J
:clap: :clap: :clap:

User avatar
boiler
Posts: 8835
Joined: 21 Dec 2014, 02:44

Re: search for a subfolder from its grandparent folder

Post by boiler » 24 Jun 2021, 05:15

Another lesson to be learned here is that when a script isn’t working, do some simple debugging such as placing MsgBoxes in it just to see if it’s reaching particular parts of the script. Had you done that, your MsgBox wouldn’t have shown up and you would have much more quickly determined that you weren’t even activating the hotkey from this script.

Post Reply

Return to “Ask For Help”