Recursive Limit for Loop Files Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
SirSocks
Posts: 360
Joined: 26 Oct 2018, 08:14

Recursive Limit for Loop Files

09 Apr 2021, 09:50

Hello, I would like to recursively Loop through Folders that are up to 3 folders deep.
How can a limit be applied to the Loop, Files function to only loop 3 folders deep?

Example code below does not work as intended.


All help and guidance is appreciated.

Code: Select all

Loop, Files, %A_WinDir%\system32\*.*, DR
{
StrReplace(A_LoopFileFullPath, "\", "\", DirLimit) ; this doens't work: trying to set limit to 3 folders deep.
	If (DirLimit = 3) 
		Continue 
Tooltip, % A_LoopFileFullPath
}

Esc::
ExitApp
User avatar
boiler
Posts: 16963
Joined: 21 Dec 2014, 02:44

Re: Recursive Limit for Loop Files

09 Apr 2021, 10:35

Maybe you just need > rather than =.
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: Recursive Limit for Loop Files

09 Apr 2021, 10:36

You where almost there, just two corrections:
  • %A_WinDir%\system32 also contains slashes
  • The "=" should be an ">" to prevent going deeper

Code: Select all

#SingleInstance, Force
StrReplace(A_WinDir "\system32", "\", "\", DirLimitWin)
Loop, Files, %A_WinDir%\system32\*.*, DR
{
	StrReplace(A_LoopFileFullPath, "\", "\", DirLimit)
	If (DirLimit - DirLimitWin > 3) 
		Continue 
	Tooltip, % A_LoopFileFullPath
}

Esc::
ExitApp
User avatar
SirSocks
Posts: 360
Joined: 26 Oct 2018, 08:14

Re: Recursive Limit for Loop Files

09 Apr 2021, 10:55

Thank you very much @AHK_user and @boiler . This is very helpful 😊
teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Recursive Limit for Loop Files

09 Apr 2021, 11:36

I'd say, both solutions are wrong. Actually, the Loop keeps going deeper, just doesn't show Tooltip. :)
User avatar
boiler
Posts: 16963
Joined: 21 Dec 2014, 02:44

Re: Recursive Limit for Loop Files

09 Apr 2021, 11:47

It keeps going into those folders, yes, but presumably the ToolTip would be replaced with whatever action is desired on the relevant folders, so the rest of the files in the deeper folders are in practical terms skipped, unless time is of the essence, then another method to prevent looping through the deeper folders would be required. The looping seems to occur very quickly, however.

I know you know this, btw. Just pointing it out for the record that it is a decent practical solution.
User avatar
SirSocks
Posts: 360
Joined: 26 Oct 2018, 08:14

Re: Recursive Limit for Loop Files

09 Apr 2021, 11:56

Fair point @teadrinker, with a good explanation from @boiler. I suppose loops within loops could also work. Each loop would act as the "recursive".

Code: Select all

Loop, Files, %A_WinDir%\system32\*, D 
{
	ParentFolder1 := A_LoopFileFullPath
	Loop, Files, % ParentFolder1 "\*", D
	{
		ParentFolder2 := A_LoopFileFullPath
		Loop, Files, % ParentFolder2 "\*", D
		{
		Tooltip, % A_LoopFileFullPath
		}
	}
Tooltip, % A_LoopFileFullPath
}

Esc::
ExitApp
teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Recursive Limit for Loop Files

09 Apr 2021, 12:00

boiler wrote: The looping seems to occur very quickly, however.
Not always, it depends on the nesting level. Try:

Code: Select all

start := A_TickCount
Loop, Files, C:\*, DR
   continue
MsgBox, % A_TickCount - start
teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Recursive Limit for Loop Files

09 Apr 2021, 12:07

SirSocks wrote: I suppose loops within loops could also work.
This is a possible solution, but what if you need the 35th level?
User avatar
boiler
Posts: 16963
Joined: 21 Dec 2014, 02:44

Re: Recursive Limit for Loop Files

09 Apr 2021, 12:07

Good point, @teadrinker.

I think nesting loops like that is a good approach, @SirSocks.
teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Recursive Limit for Loop Files  Topic is solved

09 Apr 2021, 12:39

As an option:

Code: Select all

arr := []
MyFunc := Func("EnumerateFolders").Bind(arr)
LoopFilesLevel("C:", MyFunc, 2, false)

for k, v in arr
   str .= (str ? "`r`n" : "") . v

MsgBox, % str

LoopFilesLevel(rootFolder, userFunc, level := 1, enumerateFiles := true) {
   Loop, Files, % rootFolder . "\*", % "D" . (enumerateFiles ? "F" : "")
   {
      %userFunc%(A_LoopFilePath)
      if (level > 1 && InStr(A_LoopFileAttrib, "D"))
         %A_ThisFunc%(A_LoopFilePath, userFunc, level - 1, enumerateFiles)
   }
}

EnumerateFolders(arr, filePath) {
   arr.Push(filePath)
}
User avatar
SirSocks
Posts: 360
Joined: 26 Oct 2018, 08:14

Re: Recursive Limit for Loop Files

13 May 2021, 07:28

@teadrinker Your function works very well, It's also very fast. 👍

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, Google [Bot] and 323 guests