How to List All Folders In a Listbox

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

How to List All Folders In a Listbox

Post by AlFlo » 26 Jan 2022, 14:54

I'm trying to have all of the folders in a given directory show in a listbox. This is what I have so far, but it lists FILES instead of FOLDERS:

Code: Select all

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

#SingleInstance Force


;To cancel, press ESCAPE or close this window. 
Gui, Add, ListBox, vListBox_pdf x16 y10 w370 h125,

Gui, Add, Button, Default, OK

Loop, C:\Users\rest of path for directory I want to search*
{ 
   GuiControl,, ListBox_pdf, %A_LoopFileFullPath% 
} 

Gui, Show 
return 
 
ButtonOK: 
GuiControlGet, ListBox_pdf  		; Retrieve pdf selection. 

MsgBox, 4,, Open Current Selections:`n`n%ListBox_pdf%
IfMsgBox, No 
   return 

GuiClose: 
GuiEscape: 
ExitApp
How do I get it to list FOLDERS instead of FILES?


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

Re: How to List All Folders In a Listbox

Post by AlFlo » 26 Jan 2022, 15:39

Thanks, FlyingDman. So I want Loop, FilePattern, 2, 1 to get only folders and include subfolders.

But I'm clueless about where to put that in the code. I tried this:

Code: Select all

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

#SingleInstance Force


;To cancel, press ESCAPE or close this window. 
Gui, Add, ListBox, vListBox_pdf x16 y10 w370 h125,

Gui, Add, Button, Default, OK

Loop, FilePattern, 2, 1, C:\Users\rest of path for directory I want to search*  ; HERE IS WHERE I TRIED TO PUT THE FOLDERS ONLY CODE
{ 
   GuiControl,, ListBox_pdf, %A_LoopFileFullPath% 
} 

Gui, Show 
return 
 
ButtonOK: 
GuiControlGet, ListBox_pdf  		 

MsgBox, 4,, Open Current Selections:`n`n%ListBox_pdf%
IfMsgBox, No 
   return 

GuiClose: 
GuiEscape: 
ExitApp
But that didn't do anything. Where do I put the 2,1 to specify folders only and include subfolders?

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: How to List All Folders In a Listbox

Post by flyingDman » 26 Jan 2022, 16:21

You are still trying to use the old syntax. This is what you need:

Code: Select all

loop, files, C:\Users\xxx\*.* , D
	msgbox % A_LoopFileFullPath

14.3 & 1.3.7

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

Re: How to List All Folders In a Listbox

Post by AlFlo » 26 Jan 2022, 17:27

FlyingDman,

Works great, but I can't get subfolders in any useful way ...

Specifically, if I use

Code: Select all

loop, files, C:\filepath\*.* , D
It just gets folders, and not subfolders. But if I use

Code: Select all

loop, files, C:\filepath\*.* , DR
It lists all the folders first, and then all of the subfolders from various folders without any organization afterwards, like this:

Folder 1
Folder 2
Folder 3
Folder 4
Subfolder A (from Folder 1)
Subfolder B (from Folder 1)
Subfolder A (from Folder 2)
Subfolder B (from Folder 2)
Subfolder A (from Folder 3)
Subfolder B (from Folder 3)
Subfolder A (from Folder 4)
Subfolder B (from Folder 4)

What I'm trying to do is show folders and subfolders in a logical manner, organized like so:

Folder 1
Subfolder A (from Folder 1)
Subfolder B (from Folder 1)
etc.

Folder 2
Subfolder A (from Folder 2)
Subfolder B (from Folder 2)
etc.

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

Re: How to List All Folders In a Listbox

Post by boiler » 26 Jan 2022, 19:31

This will result in the folders and their sub-folders being listed in that order:

Code: Select all

FileList := ""
loop, Files, C:\filepath\*.*, DR
	FileList .= A_LoopFileFullPath "`n"
FileList := RTrim(FileList, "`n")
Sort, FileList, D`n
MsgBox, % FileList

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

Re: How to List All Folders In a Listbox

Post by AlFlo » 26 Jan 2022, 20:53

Thank you. How do I fit that within my code to show these folders and subfolders into a listbox:

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 y10 w370 h125,

Gui, Add, Button, GMeButton, MeButton

FileList := ""
loop, Files, C:\Users\RestOfFilePath\*.*, DR
	FileList .= A_LoopFileFullPath "`n"
FileList := RTrim(FileList, "`n")
Sort, FileList, D`n

{ 
   GuiControl,, ListBox_ClList, %A_LoopFileFullPath% 
} 

ListBox_ClList:
if A_GuiControlEvent <> DoubleClick
	return
GuiControlGet, Action  ; Retrieve the ListBox's current selection.
Sleep, 200
SendInput, %ListBox_ClList%
Return
 
InputUTI: 
Gui, Submit, NoHide
Send, !{Esc}
Sleep, 200
SendInput, %ListBox_ClList%

MeButton:

Gui, Submit, NoHide
 
Run, Explorer "%ListBox_ClList%"

Reload
Return

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

Re: How to List All Folders In a Listbox

Post by boiler » 26 Jan 2022, 21:57

Code: Select all

Gui, Add, ListBox, vListBox_ClList x16 y10 w370 h125

FileList := ""
loop, Files, C:\Users\RestOfFilePath\*.*, DR
	FileList .= A_LoopFileFullPath "|"
FileList := RTrim(FileList, "|")
Sort, FileList, D|
GuiControl,, ListBox_ClList, %FileList% 
Gui, Show
return

GuiClose:
ExitApp

A lot of the stuff in your script below where this part would go doesn't really make sense, so I left that out. It looks like you're using the variable ListBox_ClList in ways that it shouldn't be used. That's the list of all the items, and it looks like you are trying to use it as the selection. Be careful adding and connecting to the rest of it as it currently appears, or you'll get some unexpected results. The above script can run as shown to show you how to load the listbox itself. How you get the selection and what you do with it needs work.
Last edited by boiler on 27 Jan 2022, 14:31, edited 1 time in total.

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

Re: How to List All Folders In a Listbox

Post by AlFlo » 27 Jan 2022, 13:59

Thank you. How do I trim the beginning of the path name from what shows in the Listbox? 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, vClList x16 y10 w1000 h125

FileList := ""
loop, Files, C:\Users\RestofBeginningofFilePath\*.*, DR
	FileList .= A_LoopFilePath "|"
                                                                         
{
    for each, filePart in StrSplit(StrReplace(A_LoopFileName, ext), [A_Space, "C:\Users\RestofBeginningofFilePath\"])    
        if (filePart != "")                                                           
            shortName .= SubStr(filePart, 1, 1)                                        
	FileMove, % A_LoopFileName, % shortName . ext                                      
    output .= shortName . ext . ","                                                     
    shortName := ""                                                                   
}

FileAppend, % RTrim(output, "C:\Users\RestofBeginningofFilePath\"),  *.*

FileList := RTrim(FileList, "C:\Users\RestofBeginningofFilePath\|")
 
Sort, FileList, D`n

GuiControl,, ClList, %FileList% 
Gui, Show
return

GuiClose:
ExitApp
 

User avatar
Smile_
Posts: 857
Joined: 03 May 2020, 00:51

Re: How to List All Folders In a Listbox

Post by Smile_ » 27 Jan 2022, 14:22

When you append to FileList use this FileList .= A_LoopFileName "|" instead, and then you don't have to trim anything.

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

Re: How to List All Folders In a Listbox

Post by boiler » 27 Jan 2022, 14:31

Like this:

Code: Select all

Gui, Add, ListBox, vListBox_ClList x16 y10 w370 h125

BasePath := "C:\Users\RestOfFilePath"
FileList := ""
loop, Files, % BasePath "*.*", DR
	FileList .= A_LoopFileFullPath "|"
FileList := RTrim(FileList, "|")
Sort, FileList, D|
FileList := StrReplace(FileList, BasePath)
GuiControl,, ListBox_ClList, %FileList% 
Gui, Show
return

GuiClose:
ExitApp

Note that I just now edited the script in my previous post because it had a `n where it should have had a | due to a copy/paste error before. The version in this post is correct.

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

Re: How to List All Folders In a Listbox

Post by boiler » 27 Jan 2022, 14:33

Smile_ wrote: When you append to FileList use this FileList .= A_LoopFileName "|" instead, and then you don't have to trim anything.
That would only include the file names (i.e., the last folder in the path). It wouldn't include the subfolders in the paths beyond the base path, which it needs to show. This is meant to show nested folders grouped under parent folders. The above change would only show one level on each line.

User avatar
Smile_
Posts: 857
Joined: 03 May 2020, 00:51

Re: How to List All Folders In a Listbox

Post by Smile_ » 27 Jan 2022, 14:38

I know
That is what I thought when I saw that he wrote
trim the beginning of the path name
Yeah yours make more sense.

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

Re: How to List All Folders In a Listbox

Post by boiler » 27 Jan 2022, 14:40

Especially when they're sorted afterwards, because then they wouldn't be grouped by their parent folders. They would be a mix depending on how they happen to fall alphabetically, which is why I didn't strip the base path from it until after the sort.

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

Re: How to List All Folders In a Listbox

Post by AlFlo » 27 Jan 2022, 16:38

Thank you, Boiler ... works like a champ! Now I just have to figure out how to choose my selection from the FileList as a variable. Here's my latest try:

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 y10 w1000 h125

BasePath := "C:\Users\RestOfFilePath\"
FileList := ""
loop, Files, % BasePath "*.*", DR
	FileList .= A_LoopFileFullPath "|"
FileList := RTrim(FileList, "|")
Sort, FileList, D|
FileList := StrReplace(FileList, BasePath)
GuiControl,, ListBox_ClList, %FileList% 
Gui, Show
return

; HERE'S MY I START TRYING TO PLACE MY SELECTION INTO A VARIABLE.

if A_GuiControlEvent <> Normal
	return
GuiControlGet, Action  ; Retrieve the ListBox's current selection.
Sleep, 200
SendInput, %ListBox_ClList%
Return
 

GuiClose:
ExitApp

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

Re: How to List All Folders In a Listbox

Post by boiler » 27 Jan 2022, 17:19

This shows the selected item text that gets stored in the variable Selection in a ToolTip:

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 gSelect x16 y10 w370 h125

BasePath := "C:\Users\RestOfFilePath\"
FileList := ""
loop, Files, % BasePath "*.*", DR
	FileList .= A_LoopFileFullPath "|"
FileList := RTrim(FileList, "|")
Sort, FileList, D|
FileList := StrReplace(FileList, BasePath)
GuiControl,, ListBox_ClList, %FileList% 
Gui, Show
return

Select:
	GuiControlGet, Selection,, ListBox_ClList
	ToolTip, % Selection
Return

GuiClose:
ExitApp

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

Re: How to List All Folders In a Listbox

Post by AlFlo » 27 Jan 2022, 18:47

Fantastic, thank you Boiler! Is there any way of the make the selection a double-click, instead of single-click?

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

Re: How to List All Folders In a Listbox

Post by boiler » 27 Jan 2022, 19:19

AlFlo wrote: Fantastic, thank you Boiler!
You're welcome.

AlFlo wrote: Is there any way of the make the selection a double-click, instead of single-click?
Yes, change the Select subroutine to this:

Code: Select all

Select:
	if (A_GuiEvent = "DoubleClick") {
		GuiControlGet, Selection,, ListBox_ClList
		ToolTip, % Selection
	}
Return

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

Re: How to List All Folders In a Listbox

Post by AlFlo » 27 Jan 2022, 22:56

Thank you, Boiler. I never did get the double click working. But I wrote an even more useful (for me) script, where I've got the FileList with 3 buttons ... does different stuff depending on which button I push.

Thanks again!

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

Re: How to List All Folders In a Listbox

Post by boiler » 27 Jan 2022, 23:35

If you were using the script I posted before or something based on it, you would just replace the Select subroutine with this new one. I don’t see what else would be needed to get it working. But that makes sense to base it off of buttons after making the selection.

Post Reply

Return to “Ask for Help (v1)”