Jump to content

Filebrowser with TreeView


nuki
 Share

Recommended Posts

hey guys =)

for a new project i need a filebrowser like FileSelectFolder() but i need it selfmade as a TreeView... so im looking for a system wich loops all drives (DriveGetDrive("FIXED"), creates main trees with them (easy), loops all files and folders inside of them puts them as sub treeviews (easy too), finds out wich files of them are folders (FileGetAttrib ("DIRECTORY")) get all files inside of them and puts them in treeview and loop the folders again as subtrees etc etc etc and so on...(impossible for me)

problem is im just too dumb to create an infinite loop wich loops all these folders and files to the treeview...

i'd be glad if someone could help me with an example script so i can go on... thanks alot for reading :)

Edited by nuki
Link to comment
Share on other sites

What you need is called a recursive loop (in case you were'nt familiar with the term). Here's a simple recursive file listing script that can be tailored to your needs.

#include <File.au3>

$outputfile = "output.txt"
$rootdir = @ScriptDir
FileOpen($outputfile,2)
_SearchFolder($rootdir)
FileClose($outputfile)

Func _SearchFolder($folder)
    $files = _FileListToArray($folder,"*",1)
    $folders = _FileListToArray($folder,"*",2)
    _FileFunc($files,$folder)
    _FolderFunc($folders,$folder)
EndFunc

Func _FileFunc($files,$folder)
    For $i = 1 To UBound($files)-1
        FileWrite($outputfile, $folder & "\" & $files[$i] & @CRLF)
    Next
EndFunc

Func _FolderFunc($folders,$parentdir)
    For $i = 1 To UBound($folders)-1
        _SearchFolder($parentdir & "\" & $folders[$i])
    Next    
EndFunc

You'll also want to add some sort of "level" mechanism to ease the creation of sub items in your treeview. Lemme know if I can give a had or explain better.

Edited by spudw2k
Link to comment
Share on other sites

i just put together another onereal quick with the level mechanism. :) Hope these help.

#include <File.au3>

$outputfile = "output.txt"
$rootdir = @ScriptDir
FileOpen($outputfile,2)
_SearchFolder($rootdir,0)
FileClose($outputfile)

Func _SearchFolder($folder,$level)
    $files = _FileListToArray($folder,"*",1)
    $folders = _FileListToArray($folder,"*",2)
    _FileFunc($files,$level)
    _FolderFunc($folders,$folder,$level)
EndFunc

Func _FileFunc($files,$level)
    For $i = 1 To UBound($files)-1
        For $x = 0 to $level
            FileWrite($outputfile,"*")
        Next
        FileWrite($outputfile, $files[$i] & @CRLF)
    Next
EndFunc

Func _FolderFunc($folders,$parentdir,$level)
    For $i = 1 To UBound($folders)-1
        _SearchFolder($parentdir & "\" & $folders[$i],$level+1)
    Next    
EndFunc
Link to comment
Share on other sites

hey guys =)

for a new project i need a filebrowser like FileSelectFolder() but i need it selfmade as a TreeView... so im looking for a system wich loops all drives (DriveGetDrive("FIXED"), creates main trees with them (easy), loops all files and folders inside of them puts them as sub treeviews (easy too), finds out wich files of them are folders (FileGetAttrib ("DIRECTORY")) get all files inside of them and puts them in treeview and loop the folders again as subtrees etc etc etc and so on...(impossible for me)

problem is im just too dumb to create an infinite loop wich loops all these folders and files to the treeview...

i'd be glad if someone could help me with an example script so i can go on... thanks alot for reading :)

I hope this UDF helped you. >_<
Link to comment
Share on other sites

hi again, i tried alot of things and other methods to do it and i decided to use spudw2k's method, but i have some problems... i tried alot of different things but nothing wants to work :/ would be cool if someone could help me with it

#include <GUIConstantsEx.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#Include <GuiTreeView.au3>
#Include <File.au3>

$gui = GUICreate("File Browser", 362, 378, 193, 125)
$tree = GUICtrlCreateTreeView(0, 0, 361, 377)

GUICtrlSetColor($tree, 0x00FF00)
GUICtrlSetBkColor($tree,0x000000)
GUISetState(@SW_SHOW)

$test = _GUICtrlTreeView_AddChild($tree,"","C:")

_SearchFolder($test)

While 1 
    Sleep(1)
WEnd

Func _SearchFolder($folder)
    Opt("GUIDataSeparatorChar", "\")
    $treetest = _GUICtrlTreeView_GetTree($tree,$folder)
    $files = _FileListToArray($treetest,"*",1)
    $folders = _FileListToArray($treetest,"*",2)
    _FileFunc($files,$folder)
    _FolderFunc($folders,$folder)
EndFunc

Func _FileFunc($files,$folder)
    For $i = 1 To UBound($files)-1
        _GUICtrlTreeView_AddChild($tree,$folder,$files[$i])
    Next
EndFunc

Func _FolderFunc($folders,$parentdir)
    For $i = 1 To UBound($folders)-1
        $getnewid = _GUICtrlTreeView_AddChild($tree,$parentdir,$folders[$i])
        _SearchFolder($getnewid)
    Next   
EndFunc
Edited by nuki
Link to comment
Share on other sites

Here's a start. :)

#include <GUIConstantsEx.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#Include <GuiTreeView.au3>
#Include <File.au3>

$winX = @DesktopWidth * .35
$winY = @DesktopHeight * .375
$gui = GUICreate("File Browser", $winX, $winY, -1, -1, $WS_SIZEBOX)
$tree = GUICtrlCreateTreeView(0, 0, $winX * .9965, $winY * .91125)
GUICtrlSetFont(-1,$winX * .0275)

GUICtrlSetColor($tree, 0x00FF00)
GUICtrlSetBkColor($tree,0x000000)
GUISetState(@SW_SHOW)

$root = _GUICtrlTreeView_AddChild($tree,"",@ScriptDir)
_SearchFolder(@ScriptDir,$root)

While 1
    $msg = GUIGetMsg()
    If $msg= -3 Then ExitLoop
WEnd
Func _SearchFolder($folder,$parent)
    $files = _FileListToArray($folder,"*",1)
    $folders = _FileListToArray($folder,"*",2)
    _FolderFunc($folders,$folder,$parent)
    _FileFunc($files,$parent)
EndFunc

Func _FileFunc($files,$parent)
    For $i = 1 To UBound($files)-1
        _GUICtrlTreeView_AddChild($tree,$parent,$files[$i])
    Next
EndFunc

Func _FolderFunc($folders,$parentdir,$parent)
    For $i = 1 To UBound($folders)-1
        $parentitem = _GUICtrlTreeView_AddChild($tree,$parent,$folders[$i])
        _SearchFolder($parentdir & "\" & $folders[$i],$parentitem)
    Next   
EndFunc
Edited by spudw2k
Link to comment
Share on other sites

Glad I could help. You can also make the folders appear above the files by running the _FolderFunc before the _FileFunc inside the _SearchFolder function.

Func _SearchFolder($folder,$parent)
    $files = _FileListToArray($folder,"*",1)
    $folders = _FileListToArray($folder,"*",2)
    _FolderFunc($folders,$folder,$parent)
    _FileFunc($files,$parent)
EndFunc
Link to comment
Share on other sites

edit: its working already i made a dumb mistake :)

umm i found some other treeview examples around this board and they are sooo much faster than mine, i dont understand why... someones knows a way how to make my script much faster?

Edited by nuki
Link to comment
Share on other sites

edit: its working already i made a dumb mistake :)

umm i found some other treeview examples around this board and they are sooo much faster than mine, i dont understand why... someones knows a way how to make my script much faster?

Post some links to the examples you're interested in and I (or someone) can look at the code
Link to comment
Share on other sites

everythings fine now... i really appreciate your help >_<

im kinda busy atm but i think i can release a testable version of my program in about a week... if your interested i can show it here :) looking for alot of ideas (after my tons of ideas are done xD) anyway because its a never ending project

Link to comment
Share on other sites

everythings fine now... i really appreciate your help >_<

im kinda busy atm but i think i can release a testable version of my program in about a week... if your interested i can show it here :) looking for alot of ideas (after my tons of ideas are done xD) anyway because its a never ending project

a great performance tweak could be to only populate folders as needed

#include <GUIConstantsEx.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#Include <GuiTreeView.au3>
#Include <File.au3>

$gui = GUICreate("File Browser", 362, 378, -1, -1)
$tree = GUICtrlCreateTreeView(0, 0, 361, 377)
GUICtrlSetFont(-1,10)
GUICtrlSetColor($tree, 0x00FF00)
GUICtrlSetBkColor($tree,0x000000)

$hImage = _GUIImageList_Create(16, 16, 5, 2)
_GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 4)
_GUIImageList_AddIcon($hImage, @SystemDir & "\shell32.dll", 54)
_GUICtrlTreeView_SetNormalImageList($tree, $hImage)

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

$root = _GUICtrlTreeView_AddChild($tree,"","C:",0)
GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    If $msg= -3 Then ExitLoop
WEnd

Func _SearchFolder($folder,$parent,$level=0)
    If $level >= 1 Then Return
    $files = _FileListToArray($folder,"*",1)
    $folders = _FileListToArray($folder,"*",2)
    _FolderFunc($folders,$folder,$parent,$level)
    _FileFunc($files,$parent)
EndFunc

Func _FileFunc($files,$parent)
    For $i = 1 To UBound($files)-1
        _GUICtrlTreeView_AddChild($tree,$parent,$files[$i],1,1)
    Next
EndFunc

Func _FolderFunc($folders,$folder,$parent,$level)
    For $i = 1 To UBound($folders)-1
        $parentitem = _GUICtrlTreeView_AddChild($tree,$parent,$folders[$i],0)
        _SearchFolder($folder & "\" & $folders[$i],$parentitem,$level+1)
    Next   
EndFunc

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR
    $hWndTreeView = GUICtrlGetHandle($tree)
    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndTreeView
            Switch $iCode
                Case -451
                    $item = _GUICtrlTreeView_GetSelection($hWndTreeView)
                    $root = $item
                    If _GUICtrlTreeView_GetChildCount($hWndTreeView,$item) <= 0 Then
                        $txt = _GUICtrlTreeView_GetText($hWndTreeView,$item)
                        Do
                            $parent = _GUICtrlTreeView_GetParentHandle($hWndTreeView,$item)
                            If $parent <> 0 Then
                                $txt = _GUICtrlTreeView_GetText($hWndTreeView,$parent) & "\" & $txt
                                $item = $parent
                            EndIf
                        Until $parent = 0
                        _SearchFolder($txt,$root)
                    EndIf
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc

edit: added icons and level mechanism. It only goes one level deep at a time. *See line 29

Edited by spudw2k
Link to comment
Share on other sites

yea but you have to double click at the folder to build the sub trees, if you click the arrows to pop it out its bugged, no doubt its a great script :) but look at rasims example, his tree is fully built in one second, if it would automatically update and would have files too in it and not only folders it would be perfect... the icons, own document dir and cd drives are cool too..

but i dont understand even a little bit while looking at his udf how he did it to modify... its like a c++ code for me xD

btw i built in a func to give out the dir with double clicking on a tree... on your example it would be:

GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")



Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
    #forceref $hWndGUI, $MsgID, $wParam
    Local $tagNMHDR, $event
    
    If $wParam = $tree Then
        $tagNMHDR = DllStructCreate("int;int;int", $lParam)
        $event = DllStructGetData($tagNMHDR, 3)
        
        If $event = $NM_DBLCLK Then OnDoubleClick()
    EndIf
EndFunc   ;==>WM_Notify_Events

Func OnDoubleClick()
    If _GUICtrlTreeView_GetSelection($tree) <> 0 Then
        $sel = _GUICtrlTreeView_GetSelection($tree)
        If StringInStr(_GUICtrlTreeView_GetText($tree, $sel), ":") <> 0 Then
        ;   _GUICtrlEdit_AppendText($input, _GUICtrlTreeView_GetText($tree, $sel) & "\")
            MsgBox(0,"",_GUICtrlTreeView_GetText($tree, $sel) & "\")
        Else
            $path = _GUICtrlTreeView_GetText($tree, $sel)
            Do
                $sel = _GUICtrlTreeView_GetParentHandle($tree, $sel)
                $path = _GUICtrlTreeView_GetText($tree, $sel) & "\" & $path
            Until StringInStr($path, ":") <> 0
        ;   _GUICtrlEdit_AppendText($input, $path)
            MsgBox(0,"",$path)
        EndIf
    EndIf
EndFunc   ;==>OnDoubleClickoÝ÷ ÚëjȦ²+ljëh×6GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")


Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
    #forceref $hWndGUI, $MsgID, $wParam
    Local $tagNMHDR, $event
    
    If $wParam = $tree Then
        $tagNMHDR = DllStructCreate("int;int;int", $lParam)
        $event = DllStructGetData($tagNMHDR, 3)
        
        If $event = $NM_DBLCLK Then OnDoubleClick()
    EndIf
EndFunc   ;==>WM_Notify_Events

Func OnDoubleClick()
    If _GUICtrlTreeView_GetSelection($tree) <> 0 Then
        $sel = _GUICtrlTreeView_GetSelection($tree)
        $text = _GUICtrlTreeView_GetText($sel)
        MsgBox(0,"",_ShellTreeView_GetSelected($tree, $text, $sel))
    EndIf
EndFunc
Edited by nuki
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...