Binding to a Folder by Using the Browse For Folder Dialog Box

Microsoft® Windows® 2000 Scripting Guide

Folder management scripts are often designed to work against any folder on a computer. Because of this, folder names and paths are typically not hard-coded into the script; instead, these values must be typed in as command-line arguments each time the script is run. This provides your script with the flexibility to work against any folder on a computer; however, it also requires you to know the exact location of each folder you want to manage and to correctly type that location before the script can run. This can be particularly cumbersome if the folder you want to manage has a name such as C:\System Administration\Administrator Tools\Diagnostics\Scripts\Network\IP Configuration.

If you are working with a single folder at a time, the Shell's Browse For Folder dialog box provides a graphical alternative to command-line arguments. Instead of requiring you to type in a folder path, the Browse For Folder dialog box (shown in Figure 11.4) allows you to select the folder from a standard Windows Explorer-like tree control.

Figure 11.4 Browse For Folder Dialog Box

sas_fil_04s

After you select a folder and click OK, your script is bound to the Folder object for the selected folder. If you are using a Shell object script, you can proceed to carry out the desired actions on that folder. If you want to pass the selected folder to the FileSystemObject or to WMI, your script can:

  1. Use the Self method to return a FolderItems object for the selected folder.

  2. Use the Path method to return the path to the folder (for example, C:\Scripts\Logs\Performance.vbs).

  3. Use the Replace method to replace each backslash in the path with a pair of backslashes (for example, C:\\Scripts\\Logs\\Performance.vbs). This must be done if you are using WMI because WQL queries require you to use double backslashes in path names.

  4. Pass the path to WMI or the FileSystemObject.

To display the Browse for Folder dialog box, use the Shell object's BrowseForFolder method along with the parameters shown in Table 11.6.

Table 11.6 BrowseForFolder Parameters

Parameter

Description

Window handle

Numeric ID to be assigned to the displayed dialog box. For scripts, this value should be 0.

Title

Text string to be displayed inside the dialog box. The title typically represents instructions to the user; in Figure 11.4, the title is "Select a folder to compress:"

Options

Optional values that can be used in constructing the dialog box. Two values are particularly useful in scripts:

  • &H10& (BIF_EDITBOX). Includes an edit box in the dialog box that allows you to type in a folder path.

  • &H4000& (BIF_BROWSEINCLUDEFILES). Shows files as well as folders in the dialog box. This allows you to bind to an individual file rather than an entire folder.

To display the standard dialog box, set the Options to 0.

Root folder

Optional parameter specifying the root folder shown at the top of the dialog box. If the root folder is set to C:\Scripts\ADSI, the folder ADSI appears at the top of the dialog box, and neither C:\Scripts nor C:\ is accessible.

For example, the following code displays the Browse For Folder dialog box with these options:

  • A window handle of 0.

  • The title, "Select a folder to compress."

  • No options for adding an edit box or displaying files as well as folders.

  • The root folder D:\. Only folders on drive D are shown in the dialog box.

Set objFolder = objShell.BrowseForFolder _
    (0, "Select a folder to compress:", 0, "D:\")

Scripting Steps

Listing 11.8 contains a script that uses the Browse For Folder dialog box to return the path of a selected folder. To carry out this task, the script must perform the following steps:

  1. Create a constant named WINDOW_HANDLE and set the value to 0. This constant is used to provide the handle to the dialog box window.

  2. Create a constant named NO_OPTIONS and set the value to 0. This constant is used to indicate that the dialog box should be displayed without any special options.

  3. Create an instance of the Shell object.

  4. Use the BrowseForFolder method to display the Browse For Folder dialog box. The method uses the following values for the four parameters:

    • WINDOW_HANDLE.

    • Select a folder to compress.

    • NO_OPTIONS.

    • C:\Scripts.

  5. Use the Self method to return a FolderItem object representing the folder selected in the dialog box.

  6. Set the variable objPath to the path of the folder (for example, C:\Scripts\Network).

  7. Use the VBScript Replace function to replace all single backslashes (\) in the path with double backslashes (\\). (For example, C:\Scripts\Network becomes C:\\Scripts\\Network.) The double backslashes are required by WMI any time you use a single backslash in a query.

  8. Create a variable to specify the computer name.

  9. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."

  10. Use the ExecQuery method to query the Win32_Directory class.

    To limit data retrieval to a specified folder, a Where clause is included restricting the returned folders to the folder selected from the Browse for Folder dialog box.

  11. For the single folder in the collection, echo the value of the Readable property.

Listing 11.8 Using the Browse For Folder Dialog Box

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Const WINDOW_HANDLE = 0
Const NO_OPTIONS = 0
Set objShell = CreateObject("Shell.Application") 
Set objFolder = objShell.BrowseForFolder _
 (WINDOW_HANDLE, "Select a folder:", NO_OPTIONS, "C:\Scripts") 
Set objFolderItem = objFolder.Self 
objPath = objFolderItem.Path
objPath = Replace(objPath, "\", "\\")
strComputer = "."
Set objWMIService = GetObject _
 ("winmgmts:" & "!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Directory WHERE Name = '" & objPath & "'")
For Each objFile in colFiles
 Wscript.Echo "Readable: " & objFile.Readable
Next