Create TreeView from flat file list Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
anotherbyte
Posts: 3
Joined: 14 Jan 2020, 03:54

Create TreeView from flat file list

26 Oct 2021, 09:50

Hello.

I'm trying to build a TreeView from a flat list of files like this:

Code: Select all

X:\Folder5\Folder9\SomeFile5.txt
X:\Folder5\Folder6\Folder7\Folder8\SomeFile4.txt
E:\Folder1\Folder4\SomeFile3.txt
E:\Folder1\Folder2\SomeFile2.txt
E:\Folder1\Folder2\Folder3\SomeFile1.txt
I want the resulting TreeView to be the same as if I was enumerating the folders with Loop (like the example from the docs).
I can't enumerate the files/folders because they don't necessarily exist. My data comes from a database in that format.
The data contains only files, no folders, and can be large (up to tens of thousands of entries), but is usually very small (dozens of entries), so inefficient/suboptimal solutions are ok.
The data is sorted (and I can have it reversed for free, if needed). I'm using it reversed in my example because my code was showing the results reversed.
Any help is greatly appreciated.


I'm pasting the code below, so it can easily be checked.
Here's how it looks. Notice that X:\Folder5\Folder9 is not a child of a parent X:\Folder5, same for E:\Folder1\Folde4 and E:\Folder1\Folder2, but, E:\Folder1\Folder2\Folder3 is properly parented.
What is happening is that I can't figure out a way to create items in the TreeView for all sub-paths of the given path. I can see when I call TV_Add for the folders that I'm not handling all cases.
I've tried quite a few ideas, so maybe I got blinded.
wrong.png
wrong.png (6.75 KiB) Viewed 582 times

Code: Select all

Removed to avoid confusion with users searching for this.
See accepted answer. It's perfect.
Last edited by anotherbyte on 04 Nov 2021, 19:24, edited 1 time in total.
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Create TreeView from flat file list  Topic is solved

26 Oct 2021, 12:49

Code: Select all

ImageListID := IL_Create(6)
Loop 6
    IL_Add(ImageListID, "shell32.dll", A_Index - 1)
Gui, main: new
Gui, Add, TreeView, vMyTreeView w600 R20 ImageList%ImageListID%
Gui, show

db =
(
X:\Folder5\Folder9\SomeFile5.txt
X:\Folder5\Folder6\Folder7\Folder8\SomeFile4.txt
E:\Folder1\Folder4\SomeFile3.txt
E:\Folder1\Folder2\SomeFile2.txt
E:\Folder1\Folder2\Folder3\SomeFile1.txt
)
TV_AddDB(DB, "\")
return

TV_AddDB(DB, Delim) {
	O:=[]
	for i, line in StrSplit(DB, "`n", "`r"){
		F:=S:=""
		for j, branch in (obj := StrSplit(line, Delim))
			S.=(S?Delim:"")branch,O[S]:=(O[S])?O[S]:TV_Add(branch,O[F], "Expand " . (j=obj.Count()?"icon0":"icon5")),F:=S
	}
}
teadrinker
Posts: 4325
Joined: 29 Mar 2015, 09:41
Contact:

Re: Create TreeView from flat file list

26 Oct 2021, 15:45

AlphaBravo wrote:

Code: Select all

Loop 6
    IL_Add(ImageListID, "shell32.dll", A_Index - 1)
Why A_Index - 1 ? It's 1-based.
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Create TreeView from flat file list

27 Oct 2021, 08:31

teadrinker wrote:
26 Oct 2021, 15:45
Why A_Index - 1 ? It's 1-based.
you're right, I just did a copy and paste from OP
anotherbyte
Posts: 3
Joined: 14 Jan 2020, 03:54

Re: Create TreeView from flat file list

27 Oct 2021, 22:06

Thanks a lot, it seems to be working!
About the "A_Index - 1", I used because shell32.dll is 0-based and I wanted to use the icon at index 0.

Thanks again, I'll report back if I notice anything.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], hiahkforum, NullRefEx and 123 guests