Jump to content

Directory to Treeview


Recommended Posts

I have been trying to get a Treeview control to load up all the folder/files in a chosen directory (Excluding INI files, and hidden files), but it just got overly complicated. It almost works, but somehow I get the wrong directory stored into $names[], its wierd because it seems to add the previous directory, and then attach a folder name totally randomly. Some directories work, some dont!

Posted Image

the code is a wreck, but if you want to take a look

The main problem appears to be in mainList(), I know its a bit confusing in what the variables are... I dont rea

#include <guiconstants.au3>
#include <array.au3>

Dim $splash, $bgcolor = 0x404040, $txtcolor = 0xff8800, $extcolor = 0xffcc00, $items[1], $names[1]

$Main = GUICreate("DirView", 500, 430, -1, -1)
$Tree = GUICtrlCreateTreeView(24+32, 4, 240,422)
$programs = GUICtrlCreateTreeViewItem("All Programs", $tree)

MakeList(@StartMenuDir, $programs,0)
MakeList(@StartMenuDir, $programs,1)

GUISetState()

while 1
    $msg = GUIGetMsg()
    Switch $msg
        case $GUI_EVENT_CLOSE
            Exit
        Case Else
            for $i = 1 to ubound($items) - 1
                if $msg = $items[$i] then ishidden($names[$i])
            Next
            sleep(20)
    EndSwitch
WEnd

Func makeList($dir, $parent, $mode = 0)
    FileChangeDir($dir)
    $search = FileFindFirstFile("*.*")
    
    While 1
        $file = FileFindNextFile($search) 
        If @error Then ExitLoop
        
        if GetExt($file) <> "folder" Then
            if $mode = 1 and getext($file) <> "INI" then    ;This is for folders
                _ArrayAdd($items, GUICtrlCreateTreeViewItem($file,$parent))
                _ArrayAdd($names, @workingdir & "\" & $file)
                guictrlsetcolor($items[Ubound($items) - 1], $txtcolor)
            EndIf 
        Else
            if $mode = 0 Then   ;This is for files
                _ArrayAdd($items, GUICtrlCreateTreeViewItem($file,$parent))
                _ArrayAdd($names, @workingdir & "\" & $file)
                guictrlsetcolor($items[Ubound($items) - 1], $extcolor)
                $tdir = $dir & "\" & $file
                MakeList($tdir, $items[Ubound($items) - 1])
                MakeList($tdir, $items[Ubound($items) - 1],1)
                $items[0] = $items[0] + 1
            endif
        EndIf
    WEnd
    fileclose($search)
EndFunc

Func GetExt($file)
    local $i 
    $i = $file
    if StringInstr($i, ".") = 0 then Return "folder" ;if file has no extension, mark as a folder
    $i = stringsplit($file, "\") ;split by directory, ie: C:\Program Files\Text.txt => C:, Program Files, Text.txt
    $i = stringsplit($i[ubound($i) - 1], ".") ;take last dir and split by period => Text.txt > text, txt > txt
    return StringUpper($i[ubound($i) - 1] ) ;return only extension in ALL CAPS
EndFunc

func ishidden($file)
    local $attrib
    $attrib = filegetattrib($file)
    msgbox(0,"",$file & @CRLF & "Properties: " & $attrib)
    If StringInStr($attrib, "H") Then return 1
    return 0
EndFunc
Link to comment
Share on other sites

Hi,

I have play with it a litle, and came with something...

The variable $dir was never changed, so the path is always (almoust) the same, just the file name changing, try this, i think it better now:

(i also have changed a litle the function GetExt(), now it's return more correct information :) )

#include <guiconstants.au3>
#include <array.au3>

Dim $splash, $bgcolor = 0x404040, $txtcolor = 0xff8800, $extcolor = 0xffcc00, $items[1], $names[1]

$Main = GUICreate("DirView", 500, 430, -1, -1)
$Tree = GUICtrlCreateTreeView(24+32, 4, 240,422)
$programs = GUICtrlCreateTreeViewItem("All Programs", $tree)

MakeList(@StartMenuDir, $programs,0)
MakeList(@StartMenuDir, $programs,1)

GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case Else
            For $i = 1 To Ubound($items) - 1
                If $msg = $items[$i] Then ishidden($names[$i])
            Next
            sleep(20)
    EndSwitch
WEnd

Func MakeList($dir, $parent, $mode = 0)
    FileChangeDir($dir)
    $search = FileFindFirstFile("*.*")
    
    While 1
        $file = FileFindNextFile($search) 
        If @error Then ExitLoop
        
        $GetExt = GetExt($dir & "\" & $file)
        
        If $GetExt <> "folder" Then
            If $mode = 1 Then
                _ArrayAdd($items, GUICtrlCreateTreeViewItem($file,$parent))
                _ArrayAdd($names, $dir & "\" & $file)
                guictrlsetcolor($items[Ubound($items) - 1], $txtcolor)
            EndIf
        ElseIf $mode = 0 And $GetExt <> "INI" And $GetExt = "folder" Then
            $dir &= "\" & $file
            _ArrayAdd($items, GUICtrlCreateTreeViewItem($file,$parent))
            _ArrayAdd($names, $dir)
            guictrlsetcolor($items[Ubound($items) - 1], $extcolor)
            fileclose($search)
            MakeList($dir, $items[Ubound($items) - 1])
            MakeList($dir, $items[Ubound($items) - 1],1)
            $items[0] += 1
        EndIf
    WEnd
    fileclose($search)
EndFunc

Func GetExt($file)
    If StringInstr(FileGetAttrib($file), "D") Then Return "folder"
    Return StringUpper(StringTrimLeft($file, StringInStr($file, ".", 0, -1))) ;return only extension
EndFunc

func ishidden($file)
    local $attrib
    $attrib = filegetattrib($file)
    msgbox(0,"",$file & @CRLF & "Properties: " & $attrib)
    If StringInStr($attrib, "H") Then return 1
    return 0
EndFunc

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Or you can get the tree selected using this method:

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case Else
            For $i = 1 To Ubound($items) - 1
                If $msg = $items[$i] Then
                    $GetTree = _GUICtrlTreeViewGetTree($Tree, "\")
                    $RootTrim = StringTrimLeft($GetTree, StringInStr($GetTree, "\"))
                    $GetTree = @StartMenuDir & "\" & $RootTrim
                    ishidden($GetTree)
                EndIf
            Next
            sleep(20)
    EndSwitch
WEnd

(This is the main loop).

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Thanks :) so far works good. One problem im not sure about, hamachi.lnk appears under startupassistant.exe which is not a folder at all. I cant recreate this though, so I guess Ill spend some time debugging this. I have tried making a treeview/directoryview hybrid before, this is the farthest I have got though.

Probrably just something small, dont have time to debug it out right now. Sorry about the delayed response -- just finished my finals for college :)

Edited by Rad
Link to comment
Share on other sites

Hi,

Yes i saw the bug too; this should be fast on bigger directories too [avoids _ArrayAdd (redims a lot, slow?)]; [but is nice to see it] [i have limited to 4000 due to using controls in TreeView at present]

;dirTreeMScreator.au3; mod randallc
#include <GuiTreeView.au3>
Global $splash, $bgcolor = 0x404040, $txtcolor = 0xff8800, $extcolor = 0xffcc00, $items[4000], $names[4000], $StartDir

$Main = GUICreate("DirView", 500, 430, -1, -1)
$Tree = GUICtrlCreateTreeView(24 + 32, 4, 400, 422)
;~ $StartDir = @ScriptDir
$StartDir = @StartMenuDir
$TrreViewOfprograms = GUICtrlCreateTreeViewItem($StartDir, $Tree)
$TimerStamp = TimerInit()
MakeList($StartDir, $TrreViewOfprograms, 0)
MakeList($StartDir, $TrreViewOfprograms, 1)
ReDim $items[$items[0] + 1]
ReDim $names[$names[0] + 1]
ConsoleWrite("$TimerStamp=" & Round(TimerDiff($TimerStamp)) & @LF)
ConsoleWrite("$items[0]+1=" & $items[0] + 1 & @LF)
GUISetState()
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case Else
            If $msg >= $items[1] And $msg <= $items[UBound($items) - 1] Then
                $GetTree = _GUICtrlTreeViewGetTree($Tree, "\")
                $RootTrim = StringTrimLeft($GetTree, StringInStr($GetTree, "\"))
                $GetTree = $StartDir & "\" & $RootTrim
                ishidden($GetTree)
            EndIf
            Sleep(20)
    EndSwitch
WEnd
Func MakeList($dir, $TreeViewOfprograms, $mode = 0)
    FileChangeDir($dir)
    $search = FileFindFirstFile("*.*")
    While 1
        $file = FileFindNextFile($search);
        If @error Then ExitLoop
        $GetExt = GetExt($dir & "\" & $file)
        If $items[0] > 3998 Then ExitLoop
        If $GetExt <> "folder" And $mode = 1 And $GetExt <> "ini" Then;And $GetExt <> "lnk" And $GetExt <> "ini"
            $items[0] += 1
            $names[0] += 1
            $items[ $items[0] ] = GUICtrlCreateTreeViewItem($file, $TreeViewOfprograms)
            $names[ $names[0] ] = $dir & "\" & $file;$file
            GUICtrlSetColor($items[ $items[0]], $txtcolor)
        ElseIf $GetExt = "folder" And $mode = 0 Then;And $GetExt <> "INI"
            ConsoleWrite("$dir=" & $dir & @LF)
            $TreevieItemNow = GUICtrlCreateTreeViewItem($file, $TreeViewOfprograms)
            $items[0] += 1
            $names[0] += 1
            $items[ $items[0] ] = $TreevieItemNow
            $names[ $names[0] ] = $dir & "\" & $file;$file
            GUICtrlSetColor($items[ $items[0]], $extcolor)
            MakeList($dir & "\" & $file, $TreevieItemNow, 0)
            MakeList($dir & "\" & $file, $TreevieItemNow, 1)
        EndIf
    WEnd
    FileClose($search)
EndFunc   ;==>MakeList
Func GetExt($file)
    If StringInStr(FileGetAttrib($file), "D") Then Return "folder"
    Return StringUpper(StringTrimLeft($file, StringInStr($file, ".", 0, -1))) ;return only extension
EndFunc   ;==>GetExt

Func ishidden($file)
    Local $attrib
    $attrib = FileGetAttrib($file)
    MsgBox(0, "", $file & @CRLF & "Properties: " & $attrib)
    If StringInStr($attrib, "H") Then Return 1
    Return 0
EndFunc   ;==>ishidden
Best, Randall Edited by randallc
Link to comment
Share on other sites

or, better,

;dirTreeMScreator.au3; mod randallc
#include <GuiTreeView.au3>
Global $splash, $bgcolor = 0x404040, $txtcolor = 0xff8800, $extcolor = 0xffcc00, $items[4000], $names[4000], $StartDir

$Main = GUICreate("DirView", 500, 430, -1, -1)
$Tree = GUICtrlCreateTreeView(24 + 32, 4, 400, 422)
$StartDir = @ScriptDir
;~ $StartDir = @StartMenuDir
$TrreViewOfprograms = GUICtrlCreateTreeViewItem($StartDir, $Tree)
$TimerStamp = TimerInit()
MakeList($StartDir, $TrreViewOfprograms, 0)
MakeList($StartDir, $TrreViewOfprograms, 1)
ReDim $items[$items[0] + 1]
ReDim $names[$names[0] + 1]
ConsoleWrite("$TimerStamp=" & Round(TimerDiff($TimerStamp)) & @LF)
ConsoleWrite("$items[0]+1=" & $items[0] + 1 & @LF)
GUISetState()
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case Else
            If $msg >= $items[1] And $msg <= $items[UBound($items) - 1] Then ishidden(_GUICtrlTreeViewGetTree($Tree, "\"))
            Sleep(20)
    EndSwitch
WEnd
Func MakeList($dir, $TreeViewOfprograms, $mode = 0)
    FileChangeDir($dir)
    $search = FileFindFirstFile("*.*")
    While 1
        $file = FileFindNextFile($search);
        If @error Then ExitLoop
        $GetExt = GetExt($dir & "\" & $file)
        If $items[0] > 3998 Then ExitLoop
        If $GetExt <> "folder" And $mode = 1 And $GetExt <> "ini" Then;And $GetExt <> "lnk" And $GetExt <> "ini"
            $items[0] += 1
            $names[0] += 1
            $items[ $items[0] ] = GUICtrlCreateTreeViewItem($file, $TreeViewOfprograms)
            $names[ $names[0] ] = $file;$file$dir & "\" & 
            GUICtrlSetColor($items[ $items[0]], $txtcolor)
        ElseIf $GetExt = "folder" And $mode = 0 Then;And $GetExt <> "INI"
            $TreevieItemNow = GUICtrlCreateTreeViewItem($file, $TreeViewOfprograms)
            $items[0] += 1
            $names[0] += 1
            $items[ $items[0] ] = $TreevieItemNow
            $names[ $names[0] ] = $file;$file;$dir & "\" & 
            GUICtrlSetColor($items[ $items[0]], $extcolor)
            MakeList($dir & "\" & $file, $TreevieItemNow, 0)
            MakeList($dir & "\" & $file, $TreevieItemNow, 1)
        EndIf
    WEnd
    FileClose($search)
EndFunc   ;==>MakeList
Func GetExt($file)
    If StringInStr(FileGetAttrib($file), "D") Then Return "folder"
    Return StringUpper(StringTrimLeft($file, StringInStr($file, ".", 0, -1))) ;return only extension
EndFunc   ;==>GetExt

Func ishidden($file)
    Local $attrib
    $attrib = FileGetAttrib($file)
    MsgBox(0, "", $file & @CRLF & "Properties: " & $attrib)
    If StringInStr($attrib, "H") Then Return 1
    Return 0
EndFunc   ;==>ishidden
randall
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...