Advanced PowerBuilder

HomePrevious Lesson: Copying the Full Screen to the Clipboard
Next Lesson: Closing the Current Session And Display Login Screen

Browse For the Folder

We have functions to prompt the user to specify a file to open or save. But there is no direct function call to prompt the user to select a directory. If we need to do it purely in PowerScript, then we can do it using a ListBox, SingleLineEdit/StaticText control and DirList() function. However, the following code displays the standard Folder Browser dialog box.
FUNCTION long ufx_BrowseForFolder( REF s_BrowseDirInfo &
         lpBi ) LIBRARY "SHELL32.DLL" ALIAS FOR &
         "SHBrowseForFolder"
FUNCTION boolean ufx_GetPathFromIDList( ulong pidl, REF &
         string pszBuffer ) LIBRARY "shell32.dll" ALIAS &
         FOR "SHGetPathFromIDList"
FUNCTION boolean ufx_GetPathFromIDList( s_itemIDList &
         ppidl, REF string pszBuffer ) LIBRARY &
         "shell32.dll" ALIAS FOR "SHGetPathFromIDList"

Before you start coding, create the following three structure objects in the same order.
// Structure Name: s_BrowseDirInfo
long			hwndowner
long			pidlroot
string		pszdisplayname
string		lpsztitle
unsignedinteger	ulflags
long			lpfn
long			lparam
integer		iimage
// Structure Name: s_shitemid
unsignedinteger	cb
character		abID[1]
// Structure Name: s_itemidlist
s_shitemid		mkid
// Object: cb_browse_for_folder
// Event: Clicked
// Shell browsing flags
CONSTANT uint BIF_RETURNONLYFSDIRS	= 1
CONSTANT uint BIF_DONTGOBELOWDOMAIN	= 2
CONSTANT uint BIF_STATUSTEXT		= 4
CONSTANT uint BIF_RETURNFSANCESTORS	= 8
CONSTANT uint BIF_EDITBOX		= 16
CONSTANT uint BIF_VALIDATE		= 32
CONSTANT uint BIF_USENEWUI		= 0
CONSTANT uint BIF_BROWSEFORCOMPUTER	= 4096
CONSTANT uint BIF_BROWSEFORPRINTER	= 8192
String		ls_null, ls_name, ls_title, ls_path, ls_folder
long			ll_pidl, ll_rc, ll_null
Integer		li_image
s_BrowseDirInfo	lstr_BrowseDirInfo
SetNull( ls_null )
SetNull( ll_null )
lstr_BrowseDirInfo.hWndOwner = Handle( parent )
lstr_BrowseDirInfo.pidlRoot = ll_null
lstr_BrowseDirInfo.pszDisplayName = Space( 256 )
lstr_BrowseDirInfo.lpszTitle = "Select a Folder " + char(0)
lstr_BrowseDirInfo.ulFlags = BIF_RETURNONLYFSDIRS
lstr_BrowseDirInfo.lpfn = ll_null
lstr_BrowseDirInfo.lParam = ll_null
lstr_BrowseDirInfo.iImage = li_image
ll_pidl = ufx_BrowseForFolder( lstr_BrowseDirInfo )
IF ll_pidl > 0 THEN
	ls_path = Space( 256 )
	IF ufx_GetPathFromIDList( ll_pidl, ls_path ) THEN
			ls_folder = ls_path // Returns full path
			ls_folder = lstr_BrowseDirInfo.pszDisplayName 
     // Returns only the directory name.
		MessageBox( "Selected Folder", ls_folder )
	END IF
END IF
HomePrevious Lesson: Copying the Full Screen to the Clipboard
Next Lesson: Closing the Current Session And Display Login Screen