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

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

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

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • This works fine in Windows XP, but not in Vista.

    I ran into problems to find a good way to generate a file dialog in VBScript that works across the latest OS versions.. XP, Vista (and hopefully Win 7). I also need the user to be able to select multiple files and I always like the option to allow the user to enter the path to a directory manually (copy and paste) to avoid unnecessary click orgy's in scenarios involving deep directory structures.

    I found some stuff on the Internet, but are not fully satisfied with what I have found so far.

    "SAFRCFileDlg.FileOpen" also does not work under Vista.

    I saw an ugly workaround using Internet Explorer to create a "File" input on the fly and trigger the  open dialog. It also limits to single file selection.

    Another one uses oWord.FileDialog, which provides a great file dialog as I would love to get from within VBScript somehow without the user require to have MS Word installed.

    The WshShell.BrowseForFolder method can be tweaked that a file can be selected, but its very user unfriendly. Its good for a Folder selection dialog.

    Any suggestions? Thanks.

  • It should be noted that if a single file was selected, the whole path + file is returned as normal, so an initial check should be performed (detect if there are any spaces) first to determine if it is a single file name.

  • I find that if I select more than 8 files, I get a return of 0 (intResult = 0 in the above example).  Is this a limitaion, or am I doing somethng else wrong?

  • How do I use this to open a file in VBS?