January 28th, 2005

How Can I Show Users a Dialog Box for Selecting Files?

Hey, Scripting Guy! Question

Hey, Scripting Guy! Is there any way I can use a script to present a user with a dialog box and let him or her select a file?

— BF

SpacerHey, Scripting Guy! AnswerScript Center

Hey, BF. If you’re using Windows 2000, we don’t know of a way to do this, at least not a way that’s built into the operating system. That’s not the case with Windows XP, however. On Windows XP, you can use the UserAccounts.CommonDialog object to present users with a standard File Open dialog box. Here’s what the script looks like:

Set objDialog = CreateObject(“UserAccounts.CommonDialog”)

objDialog.Filter = “All Files|*.*” objDialog.InitialDir = “C:\” intResult = objDialog.ShowOpen

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

It’s a small script, so let’s walk through it line-by-line. We begin by creating an object reference (named objDialog) to the UserAccounts.CommonDialog object. Next we set the Filter property for the dialog box. We want to show all files, so we set the filter like so:

objDialog.Filter = “All Files|*.*”

Suppose we wanted to show only text files? Well, in that case we’d use this filter:

objDialog.Filter = “Text Files|*.txt”

You can probably see how this works: we provide a description of the file types (Text Files), toss in a “pipe separator” (|) and then use a standard wildcard to indicate all .txt files (*.txt). Want to get fancy and show.txt files by default, but then give people the option of looking at all files? Then use this code:

objDialog.Filter = “Text Files|*.txt|All Files|*.*”

Try it, and you’ll see what we mean.

We then specify the default folder. By default, we want the dialog box to show the file in the root folder of drive C, so we set the InitialDir property like this:

objDialog.InitialDir = “C:\”

Want to show files in the C:\Windows folder instead? Then use this code:

objDialog.InitialDir = “C:\Windows”

And don’t worry: this is a real File Open dialog box, so you can click around and end up anywhere you want. Just because you start in C:\Windows doesn’t mean that’s the only folder where you can open files.

Finally, we display the dialog box using this line of code:

intResult = objDialog.ShowOpen

Now we just sit and wait for the user to select a file and click OK (or – alternatively – for the user to click Cancel). If the user clicks Cancel, the variable intResult will be set to 0. In our script, we check the value of intResult and, if it’s 0, we simply use Wscript.Quit to terminate the script.

But what if the user actually selects a file and then clicks OK? In that case, intResult will be set to -1, and the FileDialog property will be set to the path name of the selected file. Our script simply echoes back the path name, meaning we’ll get output similar to this:

C:\WINDOWS\Prairie Wind.bmp

Needless to say, however, you’re not restricted to merely echoing back the file path. Instead, you could use WMI or the FileSystemObject or some other technology to bind to that file and delete it, copy it, compress it, retrieve the file properties, or do pretty much anything you can do to a file.

Well, with a script anyway.

Incidentally, you can only select a single file at a time with this approach; no holding down the Ctrl key and selecting multiple files. There is a way to select multiple files, at least on an XP machine, but we’ll have to address that in a future column.

Author

0 comments

Discussion are closed.