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

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

  • Comments 6
  • Likes
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.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Thanks for this, it was really helpful!!! but my query is, how to make that "Browse For Folder" dialog box to be active unless OK / CANCEL button is pressed, using VBScript?

    Eg: I have an .hta file which is used to get a directory using this Browse For Folder dialog box, through a browse button... I need this dialog box to be active until I press OK / CANCEL -> is this possible using VBScript? If Yes, then how?

    Thanks in Advance!!!

  • How do you go about switching the location to the Desktop?

    Thanks in advance,

    -Joe

  • I used your code, and it works great.  Problem is when I click the Make New Folder button.  It does make the folder, but VBScript still thinks it doesn't exist.  I tried putting WScript.Sleep in there etc - no dice.  If I loop around and call objShell.BrowseForFolder again, I see and can select the folder I created.

  • this worked fine until i ran an hta as an elevated user with the shell.execute command.  then the browseforfolder acted like livefree75 described.  I hit "make new folder" and the folder appears if I browse from my computer but it does not show up in the browseforfolder dialog--unless I cancel it and go back into the dialog.  Does anyone have any ideas how to make this work or how to work around using an alternate method that may happen to work in such a situation?  I need to run the script as an elevated user (the administrator)

    Thanks...

  • ' works everytime



    Option Explicit
    Dim shell

    ' WScript.Echo BrowseFolder( "D:\data\softwarelib", True )

    Set shell = wscript.CreateObject("Shell.Application")
    shell.Open BrowseFolder( "D:\data\softwarelib", True )


    Function BrowseFolder( myStartLocation, blnSimpleDialog )
    ' This function generates a Browse Folder dialog
    ' and returns the selected folder as a string.
    '
    ' Arguments:
    ' myStartLocation [string] start folder for dialog, or "My Computer", or
    ' empty string to open in "Desktop\My Documents"
    ' blnSimpleDialog [boolean] if False, an additional text field will be
    ' displayed where the folder can be selected
    ' by typing the fully qualified path
    '
    ' Returns: [string] the fully qualified path to the selected folder
    '
    '
    '
    '
    Const MY_COMPUTER = &H11&
    Const WINDOW_HANDLE = 0 ' Must ALWAYS be 0

    Dim numOptions, objFolder, objFolderItem
    Dim objPath, objShell, strPath, strPrompt

    ' Set the options for the dialog window
    strPrompt = "Select a folder:"
    If blnSimpleDialog = True Then
    numOptions = 0 ' Simple dialog
    Else
    numOptions = &H10& ' Additional text field to type folder path
    End If

    ' Create a Windows Shell object
    Set objShell = CreateObject( "Shell.Application" )

    ' If specified, convert "My Computer" to a valid
    ' path for the Windows Shell's BrowseFolder method
    If UCase( myStartLocation ) = "MY COMPUTER" Then
    Set objFolder = objShell.Namespace( MY_COMPUTER )
    Set objFolderItem = objFolder.Self
    strPath = objFolderItem.Path
    Else
    strPath = myStartLocation
    End If

    Set objFolder = objShell.BrowseForFolder( WINDOW_HANDLE, strPrompt, _
    numOptions, strPath )

    ' Quit if no folder was selected
    If objFolder Is Nothing Then
    BrowseFolder = ""
    Exit Function
    End If

    ' Retrieve the path of the selected folder
    Set objFolderItem = objFolder.Self
    objPath = objFolderItem.Path

    ' Return the path of the selected folder
    BrowseFolder = objPath
    End Function

  • In the line of code; Set objFolderItem = objFolder.Self
    objPath = objFolderItem.Path, is there a way to capture only the folder name selected versus the 'objFolderItem.Path' capturing the path ?