Function Reference


Displays a dialog box that enables the user to select a Shell folder

#include <WinAPIDlg.au3>
_WinAPI_BrowseForFolderDlg ( [$sRoot = '' [, $sText = '' [, $iFlags = 0 [, $pBrowseProc = 0 [, $lParam = 0 [, $hParent = 0]]]]]] )


$sRoot [optional] The root folder from which to start browsing.
Only the specified folder and its subfolders in the namespace hierarchy appear in the dialog box.
If this parameter is 0, the namespace root (the Desktop folder) is used.
$sText [optional] The string that is displayed above the tree view control in the dialog box.
$iFlags [optional] Flags that specify the options for the dialog box. This parameter can be a combination of the following values:

Windows 7 or later
$pBrowseProc [optional] Pointer to a callback function that the dialog box calls when an event occurs.
This function will receive one of the following event messages:
(See MSDN for more information)
$lParam [optional] The value that the dialog box passes to the callback function.
$hParent [optional] Handle to the parent window for the dialog box.

Return Value

Success: The full path for chosen folder, or an empty string if user cancels/closes the dialog.
Failure: Empty string.

See Also

Search SHBrowseForFolder in MSDN Library.


#include <APIDlgConstants.au3>
#include <MsgBoxConstants.au3>
#include <SendMessage.au3>
#include <WinAPIDlg.au3>
#include <WinAPIMem.au3>
#include <WinAPIMisc.au3>
#include <WinAPIShellEx.au3>
#include <WinAPIShPath.au3>
#include <WinAPISysWin.au3>

Local Const $sInitDir = @ProgramFilesDir

Local $hBrowseProc = DllCallbackRegister('_BrowseProc', 'int', 'hwnd;uint;lparam;ptr')
Local $pBrowseProc = DllCallbackGetPtr($hBrowseProc)

Local $pText = _WinAPI_CreateString($sInitDir)
Local $sPath = _WinAPI_BrowseForFolderDlg(_WinAPI_PathStripToRoot($sInitDir), 'Select a folder from the list below.', BitOR($BIF_RETURNONLYFSDIRS, $BIF_EDITBOX, $BIF_VALIDATE), $pBrowseProc, $pText)

If $sPath Then
        ConsoleWrite('--------------------------------------------------' & @CRLF)
        ConsoleWrite($sPath & @CRLF)


Func _BrowseProc($hWnd, $iMsg, $wParam, $lParam)
        Local $sPath

        Switch $iMsg
                Case $BFFM_INITIALIZED
                        _WinAPI_SetWindowText($hWnd, 'MyTitle')
                        _SendMessage($hWnd, $BFFM_SETSELECTIONW, 1, $lParam)
                Case $BFFM_SELCHANGED
                        $sPath = _WinAPI_ShellGetPathFromIDList($wParam)
                        If Not @error Then
                                ConsoleWrite($sPath & @CRLF)
                Case $BFFM_VALIDATEFAILED
                        MsgBox(($MB_ICONERROR + $MB_SYSTEMMODAL), 'Error', _WinAPI_GetString($wParam) & ' is invalid.', 0, $hWnd)
                        Return 1
        Return 0
EndFunc   ;==>_BrowseProc