search for a subfolder from its grandparent folder Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
JKnight_xbt33
Posts: 135
Joined: 18 Sep 2019, 02:06

search for a subfolder from its grandparent folder

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: 16767
Joined: 21 Dec 2014, 02:44

Re: search for a subfolder from its grandparent folder

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

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: 16767
Joined: 21 Dec 2014, 02:44

Re: search for a subfolder from its grandparent folder

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

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: 16767
Joined: 21 Dec 2014, 02:44

Re: search for a subfolder from its grandparent folder

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

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: 16767
Joined: 21 Dec 2014, 02:44

Re: search for a subfolder from its grandparent folder

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

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: 16767
Joined: 21 Dec 2014, 02:44

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

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

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: 16767
Joined: 21 Dec 2014, 02:44

Re: search for a subfolder from its grandparent folder

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.
suhascp
Posts: 14
Joined: 13 Nov 2021, 01:27

Re: search for a subfolder from its grandparent folder

14 Jan 2022, 05:29

Hi I have these code to open subfolder,

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
But I m not able to open the subfolder
My folder path is like this " W:\001 - Orders "
In that path have some folders like
10000 - 10099
10100 - 10199
.......
.......
.......
And so on
Inside those folders have series of folders like
10000 - 10099
10000
10001
10002
10003
....
....
....
and so on
I want to open 10045 folders by giving input as 10045
But i m not able open using those script , showing error, can some one will help on this.
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: search for a subfolder from its grandparent folder

14 Jan 2022, 06:41

You can’t use that code exactly as shown. This line has the path to the grandfather folder for the original poster’s file locations:

Code: Select all

	loop, Files, \\xxxx\shared\Departments\xxxx\xxxx\Team\xxxx\yyyy\zzzz\*, D   ;filepath is anonimised but this is grandparent folder
You would need to change it to the path of your grandfather folder:

Code: Select all

	loop, Files, W:\001 - Orders\*, D

suhascp wrote: I want to open 10045 folders by giving input as 10045
This code stops after it finds and opens a matching folder. Did you mean to say that you want it to open multiple folders instead of stopping after it finds one? If so, remove the following line:

Code: Select all

			return ; stop looking and return
suhascp
Posts: 14
Joined: 13 Nov 2021, 01:27

Re: search for a subfolder from its grandparent folder

14 Jan 2022, 09:48

Thanks i am changing that path but its showing error like
" Error : Parameter #3 invalid.
Specifically : D "


This is the code :

Code: Select all

F10::
	InputBox, Folder, Enter Folder
	loop, Files, W:\001 - Orders\*, D
	{
		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
[Mod edit: [code][/code] tags added.]

I need to open only particualr subfolder thats it.
Can please help on this.
Attachments
image.png
image.png (9.13 KiB) Viewed 957 times
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: search for a subfolder from its grandparent folder

14 Jan 2022, 09:57

Make sure you are using the latest officially released version of AHK.
suhascp
Posts: 14
Joined: 13 Nov 2021, 01:27

Re: search for a subfolder from its grandparent folder

15 Jan 2022, 05:26

Okay thanks I will check with new version of AHK
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: search for a subfolder from its grandparent folder

15 Jan 2022, 07:06

If you are using something older than 1.1.21, that’s the reason. Latest official version is 1.1.33.10. You can check by running this script:

Code: Select all

MsgBox, % A_AhkVersion

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 148 guests