Jump to content

Standard Windows FileSelectFolder dialog automatize


Zedna
 Share

Recommended Posts

I used this in my project to automatize PatchMaker application.

There is no other way to add folder to ListView

only by Button which populates standard Windows FileSelectFolder dialog.

So I did this helper function which parse desired path to be selected and

goes through TreeView control and selects/expands path parts

and finally click on OK button.

So I share it here for other users.

; note: "Vyhledat složku" is localized standard dialog title on Czech Windows
; on English Windows it will be probably "Select folder"

#include <GuiTreeView.au3>

; test code with FileSelectFolder (simulation of another process)
Run(@AutoItExe & ' /AutoIt3ExecuteLine  "FileSelectFolder(''Select folder test'', '''')"')

_SelectFolder("Vyhledat složku", "Select folder test", 'C:\Program Files\Autoit3')

Func _SelectFolder($title, $text, $path_to_select)
    ; last char musn't be '\'
    If StringRight($path_to_select,1) = '\' Then $path_to_select = StringTrimRight($path_to_select,1)
    
    WinActivate($title, $text)
    WinWaitActive($title, $text)
    $hTree = ControlGetHandle($title, $text, "SysTreeView321")
    _ParsePath($hTree, $path_to_select)
    If @error Then 
        ControlClick($title, $text, "Button2") ; Cancel
        Exit
    EndIf

    ; only test of final selection (not needed)
    ; uncomment to see final selection
;~  $hNode = _GUICtrlTreeView_GetSelection($hTree)
;~  $path_return = _GUICtrlTreeView_GetText($hTree, $hNode)
;~  MsgBox(0,'Selected node text',$path_return)

    ControlClick($title, $text, "Button1") ; OK
    WinWaitClose($title, $text)
EndFunc

; parse path to parts and goes through TreeView and selects/expands path parts
Func _ParsePath($hTree, $path)
    $path2 = StringSplit($path, '\')
    
    ; first part searching is little nonstandard
    $tmp = '(' & $path2[1] & ')' ; (C:)
    $hNext = _GUICtrlTreeView_FindItem($hTree, $tmp, True)
    If $hNext = 0 Then 
        MsgBox(48,'Error','Not found TreeView node! path=' & $path & ' part=' & $tmp)
        SetError(1)
        Return
    EndIf
    
    ; for remaining parts searching is the same
    For $i = 2 To $path2[0]
        $found = False
        _GUICtrlTreeView_Expand($hTree, $hNext)
        $hNext = _GUICtrlTreeView_GetFirstChild($hTree, $hNext)
        While $hNext <> 0
            If _GUICtrlTreeView_GetText($hTree, $hNext) = $path2[$i] Then 
                $found = True
                ExitLoop
            EndIf
            $hNext = _GUICtrlTreeView_GetNextSibling($hTree, $hNext)
        WEnd
        
        If Not $found Then
            MsgBox(48,'Error','Not found TreeView node! path=' & $path & ' part=' & $path2[$i])
            SetError(1)
            Return
        EndIf
    Next
    _GUICtrlTreeView_SelectItem($hTree, $hNext)
EndFunc

EDIT:

- 26.8.2008: updated from #include <A3LTreeView.au3> to #include <GuiTreeView.au3>

Edited by Zedna
Link to comment
Share on other sites

I used this in my project to automatize PatchMaker application.

There is no other way to add folder to ListView

only by Button which populates standard Windows FileSelectFolder dialog.

So I did this helper function which parse desired path to be selected and

goes through TreeView control and selects/expands path parts

and finally click on OK button.

So I share it here for other users.

Note: It uses TreeView functions from PaulIA's Auto3Lib library.

; note: "Vyhledat složku" is localized standard dialog title on Czech Windows
; on English Windows it will be probably "Select folder"

#include <A3LTreeView.au3>

; test code with FileSelectFolder (simulation of another process)
Run(@AutoItExe & ' /AutoIt3ExecuteLine  "FileSelectFolder(''Select folder test'', '''')"')

_SelectFolder("Vyhledat složku", "Select folder test", 'C:\Program Files\Autoit3')

Func _SelectFolder($title, $text, $path_to_select)
    ; last char musn't be '\'
    If StringRight($path_to_select,1) = '\' Then $path_to_select = StringTrimRight($path_to_select,1)
    
    WinActivate($title, $text)
    WinWaitActive($title, $text)
    $hTree = ControlGetHandle($title, $text, "SysTreeView321")
    _ParsePath($hTree, $path_to_select)
    If @error Then 
        ControlClick($title, $text, "Button2") ; Cancel
        Exit
    EndIf

    ; only test of final selection (not needed)
    ; uncomment to see final selection
;~  $hNode = _TreeView_GetSelection($hTree)
;~  $path_return = _TreeView_GetText ($hTree, $hNode)
;~  MsgBox(0,'Selected node text',$path_return)

    ControlClick($title, $text, "Button1") ; OK
    WinWaitClose($title, $text)
EndFunc

; parse path to parts and goes through TreeView and selects/expands path parts
Func _ParsePath($hTree, $path)
    $path2 = StringSplit($path, '\')
    
    ; first part searching is little nonstandard
    $tmp = '(' & $path2[1] & ')' ; (C:)
    $hNext = _TreeView_FindNode($hTree, $tmp, True)
    If $hNext = 0 Then 
        MsgBox(48,'Error','Not found TreeView node! path=' & $path & ' part=' & $tmp)
        SetError(1)
        Return
    EndIf
    
    ; for remaining parts searching is the same
    For $i = 2 To $path2[0]
        $found = False
        _TreeView_Expand($hTree, $hNext)
        $hNext = _TreeView_GetFirstChild($hTree, $hNext)
        While $hNext <> 0
            If _TreeView_GetText ($hTree, $hNext) = $path2[$i] Then 
                $found = True
                ExitLoop
            EndIf
            $hNext = _TreeView_GetNextSibling($hTree, $hNext)
        WEnd
        
        If Not $found Then
            MsgBox(48,'Error','Not found TreeView node! path=' & $path & ' part=' & $path2[$i])
            SetError(1)
            Return
        EndIf
    Next
    _TreeView_Select($hTree, $hNext)
EndFunc
Very, very smart and handy. Post also an example script
Link to comment
Share on other sites

Very, very smart and handy. Post also an example script

It's also example script.

Read these first lines:

; note: "Vyhledat složku" is localized standard dialog title on Czech Windows
; on English Windows it will be probably "Select folder"

#include <GuiTreeView.au3>

; test code with FileSelectFolder (simulation of another process)
Run(@AutoItExe & ' /AutoIt3ExecuteLine  "FileSelectFolder(''Select folder test'', '''')"')

_SelectFolder("Vyhledat složku", "Select folder test", 'C:\Program Files\Autoit3')
Edited by Zedna
Link to comment
Share on other sites

  • 1 year later...

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...