How Can I Use a Multi-Select Dialog Box in a Script?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! Awhile back you showed us how to use a File Open dialog box in our scripts. At the same time you said you’d show us how to use a multi-select dialog box. Did you just forget about the multi-select dialog box?

— AA

SpacerHey, Scripting Guy! AnswerScript Center

Hey, AA. Yes, we … forgot … about the multi-select dialog box. (Saying we forgot sounds better than saying, “No, we just irresponsibly said we’d do something and then failed to follow up on it.”) For those of you who missed our original column on adding a File Open dialog box to a script, you’ll find it here. For the rest of you, here’s a script that uses a multi-select File Open dialog box. (Note: By “multi-select” we simply mean a dialog box that allows you to select multiple files. The dialog box we showed in a previous column limited you to selecting a single file.)

Set objDialog = CreateObject(“UserAccounts.CommonDialog”)
objDialog.Filter = “VBScript Scripts|*.vbs|All Files|*.*”
objDialog.Flags = &H0200
objDialog.FilterIndex = 1
objDialog.InitialDir = “C:\Scripts”
intResult = objDialog.ShowOpen

If intResult = 0 Then Wscript.Quit Else Wscript.Echo objDialog.FileName End If

There are a few things we need to explain here, beginning with the fact that this dialog box is available only on Windows XP; the preceding script won’t work on any other version of Windows, including Windows Server 2003. Second, there’s only one difference between this and the script we showed you a couple months ago. If you want to create a multi-select dialog box, you simply need to add this line of code:

objDialog.Flags = &H0200

That’s it: set the Flags property to &H0200, and you’ll have a multi-select File Open dialog box. To select multiple files in the dialog box, just do what you usually do: click the first file, then hold down the Ctrl key and click any additional files you want to select.

Having said that, we need to point out that the File Open dialog box you get, while fully functional, isn’t as nice as the File Open dialog box you’re used to. For one thing, you don’t get the nice shortcut bar (with the icons for things like Desktop and My Documents). For another, the dialog box displays everything using short file names; in other words, the dialog box is going to look something like this:

Multi-Select Dialog Box


Fully functional, but not as aesthetically pleasing as the other File Open dialog box, the one that allows you to select only a single file.

In addition, the return value from the multi-select dialog box is a string that consists of the folder name and the short file names of all the files you selected. In this string, individual items are separated by a blank space. Thus you get back something like this:

C:\Scripts\ file_dialog.vbs file_save.vbs test.vbs

That means we need to do some additional coding here: we need to take the folder path (C:\Scripts\) and add on the individual file names in order to create valid file paths. Let’s take a look at a revised script that echoes back the file paths for each file selected:

Set objDialog = CreateObject(“UserAccounts.CommonDialog”)
objDialog.Filter = “VBScript Scripts|*.vbs|All Files|*.*”
objDialog.Flags = &H0200
objDialog.FilterIndex = 1
objDialog.InitialDir = “C:\Scripts”
intResult = objDialog.ShowOpen
If intResult = 0 Then
    Wscript.Quit
Else
    arrFiles = Split(objDialog.FileName, ” “)
    For i = 1 to Ubound(arrFiles)
        strFile = arrFiles(0) & arrFiles(i)
        Wscript.Echo strFile
    Next
End If

Here we’re using the VBScript Split command to take the string we get back from the dialog box and make it into an array (by splitting on the blank space). The first element in the array will be the folder path; the remaining elements will be the individual file names.

We then use a For Next loop to walk through the items in the array. Note that we start with the second item in the array, the item with the index number 1. (The first item in a VBScript array has an index number of 0.) We do that simply because the first item in the array isn’t actually a file name; it’s the folder path. Inside the For Next loop we combine the folder path (item 0) with the file name; that gives us a complete file path, which we store in a variable named strFile. We then echo this file path.

When we run the script, we get output similar to this:

C:\Scripts\file_dialog.vbs
C:\Scripts\file_save.vbs
C:\Scripts\test.vbs

As soon as we have file paths we can actually do something interesting with those files, like open them, delete them, compress them, or whatever else you like to do with your files.

0 comments

Discussion is closed.

Feedback usabilla icon