Share this page 

Browse for a folder using the standard Win dialogTag(s): Powerscript

[External FUNCTION declaration]
FUNCTION long SHGetPathFromIDListA (long pidl, ref string pszPath) &
LIBRARY "shell32.dll"
FUNCTION long SHBrowseForFolderA ( BROWSEINFO lpBrowseInfo)  &
LIBRARY "shell32.dll"

[structure BROWSEINFO]
howner           long
pidlroot         long
pszdisplayname   string
lpsztitle        string
ulflags          long
lpfn             long
lparam           long
iimage           long

browseinfo lst_bi
long       ll_pidl
int        li_thispos
string     ls_path, ls_xs
long       ll_rc

ls_path=space(512)      // maximum path lenght
lst_bi.howner=handle(this)  // handle of the window
lst_bi.pidlRoot = 0
lst_bi.pszdisplayname = space(260)
lst_bi.lpszTitle = "Select a folder"
lst_bi.ulFlags = 1
ll_pidl = SHBrowseForFolderA(lst_bi)
ll_rc = SHGetPathFromIDListA(ll_pidl, ls_path)
IF NOT ll_rc > 0 THEN
MessageBox("Folder selected" , ls_path)
NOTE: This may cause some memory leaks. To fix them, use the following API.
[external function declaration]
SUBROUTINE CoTaskMemFree( long lpv ) LIBRARY "ole32.dll"

SHBrowseForFolder( lst_bi )
CoTaskMemFree( lst_bi.pidlRoot )