Directory Tree Class [AHK_1.1]

Post your working scripts, libraries and tools
User avatar
maestrith
Posts: 802
Joined: 16 Oct 2013, 13:52

Directory Tree Class [AHK_1.1]

13 Jan 2014, 11:10

This is a simple class for using single or multiple treeviews with directory trees. It keeps track of the current directory and also allows for positioning, window designation, and startup directory.

I wrote this because I really dislike the way that FileSelectFolder works when giving it a starting directory. Let me know what you think.

Code: Select all

#SingleInstance,Force
d1:=new dir({win:1,w:200,h:200,start:A_ScriptDir}) ;Create a treeview in window 1 with a width and height of 200 starting in A_ScriptDir
d2:=new dir({win:1,w:200,x:"+10",h:200,start:A_Desktop}) ;Same as above but using x+10 and starting in A_Desktop
d3:=new dir({win:2,w:200,h:200,start:A_WinDir}) ;Same as above but on window 2 and starting in A_WinDir
for a,b in [[1,"xm vfirst"],[1,"x+155 vsecond"],[2,"xm vthird"]] ;setting up the buttons to display the current directories
Gui,% b.1 ":Add",Button,% "ggo " b.2,Directory
for a,b in [[1,"xm vd1"],[1,"x+160 vd2"],[2,"xm vd3"]] ;setting up the buttons to refresh the current directories
Gui,% b.1 ":Add",Button,% "grefresh " b.2,Refresh
Gui,1:Show,% "x" (A_ScreenWidth/2)-300,Directory 1
Gui,2:Show,% "x" (A_ScreenWidth/2)+150,Directory 2
return
GuiClose:
2GuiClose:
ExitApp
return
go:
result:={first:d1.last,second:d2.last,third:d3.last} ;creates an object with all of the directories
MsgBox,% result[A_GuiControl] ;display the directory
return
refresh:
%A_GuiControl%["refresh"]() ;would normally be done d1.refresh() but I wanted to make the code smaller
return
class dir{
	static con:=[]
	__New(info){
		static
		win:=info.win?info.win:1,start:=info.start,pos:=""
		for a,b in ["x","y","w","h"]
		if info[b]
		pos.=b info[b] " "
		Gui,%win%:Add,TreeView,%pos% gtree AltSubmit hwndhwnd
		GuiControl,%win%:+v%hwnd%,%hwnd%
		Gui,%win%:Default
		this.win:=info.win?info.win:1,dir.con[hwnd]:=this,this.hwnd:=hwnd
		this.refresh(),this.start(start)
	}
	refresh(){
		start:=this.getdir(),this.tv(),TV_Delete(),dirs:=[]
		DriveGet,List,List
		Loop,Parse,list
		dirs[A_LoopField ":"]:=0
		this.populate(dirs),this.start(start)
	}
	start(start){
		ll:=strsplit(start,"\")
		for a,b in ll{
			index:=A_Index
			while,ss:=TV_GetNext(ss,"F"),TV_GetText(dd,ss),TV_GetText(text,TV_GetChild(ss))
			if (dd=b){
				if (text=""&&TV_GetChild(ss))
				TV_Delete(TV_GetChild(ss)),this.populate({this.getdir(ss):ss})
				TV_Modify(ss,"Expand VisFirst Focus Select")
				if ll.MaxIndex()=index
				break
			}
		}
	}
	populate(info){
		this.tv()
		GuiControl,-Redraw,% this.hwnd
		for a,b in info{
			if !(b)
			root:=TV_Add(a),this.addsub({dir:a,tv:root})
			else
			Loop,%a%\*.*,2
			{
				if A_Index=100
				SplashTextOn,,50,Please Wait,There are quite a few directories here
				root:=TV_Add(A_LoopFileName,b),this.addsub({dir:A_LoopFileFullPath,tv:root})
			}
		}
		GuiControl,+Redraw,% this.hwnd
		SplashTextOff
	}
	addsub(info){
		Loop,% info.dir "\*.*",2
		return this.tv(),TV_Add("",info.tv)
	}
	tree(){
		tree:
		tt:=dir.con[A_GuiControl],TV_GetText(text,TV_GetChild(A_EventInfo))
		if (A_GuiControlEvent="+"&&text="")
		TV_Delete(TV_GetChild(A_EventInfo)),tt.populate({tt.getdir(A_EventInfo):A_EventInfo}),TV_Modify(A_EventInfo,"VisFirst")
		tt.last:=tt.getdir(A_EventInfo)
		return
	}
	getdir(tv=""){
		this.tv(),tv:=tv?tv:TV_GetSelection(),TV_GetText(dd,tv)
		while,tv:=TV_GetParent(tv),TV_GetText(text,(tv))
		dd:=text "\" dd
		return dd
	}
	tv(){
		Gui,% this.win ":TreeView",% this.hwnd
	}
}
Latest code can also be downloaded here
Last edited by maestrith on 20 Jan 2014, 13:04, edited 10 times in total.
John H Wilson III 05/29/51 - 03/01/2020. You will be missed.AHK Studio OSDGUI Creator
Donations
Discord
All code is done on a 64 bit Windows 10 PC Running AutoHotkey x32
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: Directory Tree Class [AHK_1.1]

14 Jan 2014, 00:58

thanks, very clean and useful. i am thinking of some real applications waiting for this.
User avatar
maestrith
Posts: 802
Joined: 16 Oct 2013, 13:52

Re: Directory Tree Class [AHK_1.1]

14 Jan 2014, 07:04

Guest10 wrote:thanks, very clean and useful. i am thinking of some real applications waiting for this.
No problem :) I would be interested to see what you do with it.
John H Wilson III 05/29/51 - 03/01/2020. You will be missed.AHK Studio OSDGUI Creator
Donations
Discord
All code is done on a 64 bit Windows 10 PC Running AutoHotkey x32
User avatar
maestrith
Posts: 802
Joined: 16 Oct 2013, 13:52

Re: Directory Tree Class [AHK_1.1]

14 Jan 2014, 14:50

Update:
Added:
-Refresh Directory
Removed:
-A bunch of bloat
-Memory usage
Also improved the speed a bit.
John H Wilson III 05/29/51 - 03/01/2020. You will be missed.AHK Studio OSDGUI Creator
Donations
Discord
All code is done on a 64 bit Windows 10 PC Running AutoHotkey x32
User avatar
Sjc1000
Posts: 39
Joined: 02 Oct 2013, 02:07

Re: Directory Tree Class [AHK_1.1]

18 Jan 2014, 03:56

Very nice... Really fast ( for me anyway ).
I always feel compelled to run your neat as classes, they're always good. :D
Please find me on the IRC if you have any questions, I'm never on the forum anymore.
User avatar
maestrith
Posts: 802
Joined: 16 Oct 2013, 13:52

Re: Directory Tree Class [AHK_1.1]

18 Jan 2014, 08:14

Sjc1000 wrote:Very nice... Really fast ( for me anyway ).
I always feel compelled to run your neat as classes, they're always good. :D
aww :) thanks Sjc1000. Thanks for giving it a try. hopefully you can get some use out of it.
John H Wilson III 05/29/51 - 03/01/2020. You will be missed.AHK Studio OSDGUI Creator
Donations
Discord
All code is done on a 64 bit Windows 10 PC Running AutoHotkey x32
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: Directory Tree Class [AHK_1.1]

18 Jan 2014, 13:24

so classes are used in this script? :geek:
User avatar
maestrith
Posts: 802
Joined: 16 Oct 2013, 13:52

Re: Directory Tree Class [AHK_1.1]

18 Jan 2014, 14:01

Guest10 wrote:so classes are used in this script? :geek:
Yes there is 1 class named dir. It takes care of all of the directories that you use. I have not used more than 3 but with 3 it is pretty quick :)
John H Wilson III 05/29/51 - 03/01/2020. You will be missed.AHK Studio OSDGUI Creator
Donations
Discord
All code is done on a 64 bit Windows 10 PC Running AutoHotkey x32
garry
Posts: 2193
Joined: 22 Dec 2013, 12:50

Re: Directory Tree Class [AHK_1.1]

18 Jan 2014, 14:55

Let me know what you think.
just great , maestrith, have lack of knowledge to programming like this ....
User avatar
maestrith
Posts: 802
Joined: 16 Oct 2013, 13:52

Re: Directory Tree Class [AHK_1.1]

18 Jan 2014, 14:57

garry wrote:
Let me know what you think.
just great , maestrith, have lack of knowledge to programming like this ....
Hopefully it helps you :)
John H Wilson III 05/29/51 - 03/01/2020. You will be missed.AHK Studio OSDGUI Creator
Donations
Discord
All code is done on a 64 bit Windows 10 PC Running AutoHotkey x32
garry
Posts: 2193
Joined: 22 Dec 2013, 12:50

Re: Directory Tree Class [AHK_1.1]

18 Jan 2014, 15:31

script is useful, and maybe I can learn something from your script
User avatar
maestrith
Posts: 802
Joined: 16 Oct 2013, 13:52

Re: Directory Tree Class [AHK_1.1]

18 Jan 2014, 15:33

garry wrote:script is useful, and maybe I can learn something from your script
Let me know if I can help.
John H Wilson III 05/29/51 - 03/01/2020. You will be missed.AHK Studio OSDGUI Creator
Donations
Discord
All code is done on a 64 bit Windows 10 PC Running AutoHotkey x32
User avatar
maestrith
Posts: 802
Joined: 16 Oct 2013, 13:52

Re: Directory Tree Class [AHK_1.1]

19 Jan 2014, 21:03

Updated:
-Fixed an issue with startup directory not stopping when it gets to the directory.
John H Wilson III 05/29/51 - 03/01/2020. You will be missed.AHK Studio OSDGUI Creator
Donations
Discord
All code is done on a 64 bit Windows 10 PC Running AutoHotkey x32
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: Directory Tree Class [AHK_1.1]

20 Jan 2014, 02:20

i tested the latest update and it is super snappy! :ugeek:
User avatar
maestrith
Posts: 802
Joined: 16 Oct 2013, 13:52

Re: Directory Tree Class [AHK_1.1]

20 Jan 2014, 09:16

Guest10 wrote:i tested the latest update and it is super snappy! :ugeek:
Thank you for testing it out. I can not think of anything else to do with it :)
John H Wilson III 05/29/51 - 03/01/2020. You will be missed.AHK Studio OSDGUI Creator
Donations
Discord
All code is done on a 64 bit Windows 10 PC Running AutoHotkey x32

Return to “Scripts and Functions”

Who is online

Users browsing this forum: blue83, Rxman, WatsonEnterprises and 61 guests