June 17th, 2005

How Can I Show Users a Dialog Box That Only Lets Them Select Folders?

Hey, Scripting Guy! Question

Hey, Scripting Guy! In previous columns you’ve shown us how to present users with a dialog box that enables them to select files. Is there any way to show them a dialog box that only lets users select folders?

— LP

SpacerHey, Scripting Guy! AnswerScript Center

Hey, LP. You know, you’ve come to the right place: the Scripting Guys have just what the doctor ordered.

Actually, we have the BrowseForFolder method, which is part of the Windows Shell object. If that’s the sort of thing your doctor is prescribing you might think very seriously about getting a second opinion.

But while we’d be a bit suspicious of a doctor who’s prescribing the BrowseForFolder method we have no qualms about prescribing this ourselves. Let’s show you a sample script, and then we’ll talk about how it works:

Const WINDOW_HANDLE = 0
Const OPTIONS = 0

Set objShell = CreateObject(“Shell.Application”) Set objFolder = objShell.BrowseForFolder _ (WINDOW_HANDLE, “Select a folder:”, OPTIONS, “C:\”)

If objFolder Is Nothing Then Wscript.Quit End If

Set objFolderItem = objFolder.Self objPath = objFolderItem.Path

Wscript.Echo objPath

We begin by defining a pair of constants: WINDOW_HANDLE and OPTIONS. The WINDOW_HANDLE constant represents a numeric ID that needs to be assigned to the dialog box we’re going to display; for scripts this value should always be 0. Setting OPTIONS to 0 means we’re going to display a very simple dialog box, one that limits users to selecting from a list of folders. Alternatively, we could have set OPTIONS to &H10&. In that case our dialog box would include a text area where users could type a folder path.

After defining our constants we create an instance of the Shell.Application object and then use this code to display a browse-for-folder dialog box:

Set objFolder = objShell.BrowseForFolder _
    (WINDOW_HANDLE, “Select a folder:”, OPTIONS, “C:\”)

As you can see, we merely call the BrowseForFolder method, passing four parameters:

WINDOW_HANDLE, which, as we noted, is the numeric ID assigned to the dialog box window.

The text string Select a folder:, which will serve as the instructional message displayed in the dialog box.

OPTIONS, the constant representing the options used in constructing the dialog box.

C:\, which serves as the root folder for our dialog box. The dialog box will open in C:\ and will not allow you to select any file locations higher up in the tree (for example, you can’t select My Computer). Had we set the root folder to C:\Scripts then the user would be allowed to select only the folder C:\Scripts and any of its subfolders.

That code will result in a dialog box similar to this appearing on screen:

Browse For Folder Dialog Box


(If you’re wondering, yes, you’ve seen this dialog box before. Many Windows applications make use of this same method and same dialog box.)

At this point our script pauses, waiting for the user to either select a folder and click OK or to click Cancel. When the user does one of these two things the dialog box is dismissed and the actions are stored in the object reference objFolder.

So how do we know whether the user selected a folder and clicked OK or whether the user simply clicked Cancel? That’s what this block of code is for:

If objFolder Is Nothing Then
    Wscript.Quit
End If

This code checks our object reference (objFolder) to see if it happens to be equal to a real object (that’s what the Nothing keyword is for). If objFolder is equal to Nothing that means the user clicked Cancel; in that case we simply use Wscript.Quit to exit the script. If objFolder is not equal to Nothing then objFolder must refer to a real object; therefore the script continues on its merry way.

The next two lines of code are required due to a quirk in the Shell object:

Set objFolderItem = objFolder.Self
objPath = objFolderItem.Path

When a user selects a folder and clicks OK they get back an instance of the Shell Folder object. For some reason, however, you can’t do anything with a Shell Folder object; if we want to retrieve the path to the selected folder we have to use a FolderItem object instead. (Why? We have no idea.) Therefore our first line of code uses the Self method to return a FolderItem object that happens to be identical to our Folder object. The second line of code then stores the Path to that FolderItem object in a variable named objPath. A bit awkward, but it works.

Finally, we echo back the path to the selected folder and we’re done:

Browse For Folder Dialog Box


As we noted, our sample dialog box uses C:\ as the root folder and won’t let you select folders located anywhere else on the computer. Sometimes that’s good; that forces users to choose from a certain set of folders. At other times, however, you’d like to give users the opportunity to select any folder located anywhere on the file system. Is that possible?

You bet it is. We won’t go into the details of this modified script, but this one sets My Computer as the root folder:

Const MY_COMPUTER = &H11&
Const WINDOW_HANDLE = 0
Const OPTIONS = 0

Set objShell = CreateObject(“Shell.Application”) Set objFolder = objShell.Namespace(MY_COMPUTER) Set objFolderItem = objFolder.Self strPath = objFolderItem.Path

Set objShell = CreateObject(“Shell.Application”) Set objFolder = objShell.BrowseForFolder _ (WINDOW_HANDLE, “Select a folder:”, OPTIONS, strPath)

If objFolder Is Nothing Then Wscript.Quit End If

Set objFolderItem = objFolder.Self objPath = objFolderItem.Path

Wscript.Echo objPath

As you might expect, the resulting dialog box gives you plenty of options to choose from:

Browse For Folder Dialog Box


Just what you needed. And, hopefully, not what the doctor ordered.

Author

0 comments

Discussion are closed.