VBS Displays the code for the Select File or Folder dialog box _vbs

Source: Internet
Author: User

A dialog box showing the Select File

Q: Hi, Scripting guy! Is there any way that I can use a script to show the User a dialog box for the user to select a file to use?

answer: hello. |
If you are using Windows 2000, we do not know how to do this, at least not in the operating system.
But if you are using Windows XP, the situation is different. On Windows XP, you can use the "useraccounts.commondialog" object to display a standard File Open dialog box to the user.

You can use a script that resembles the following code:

Copy Code code as follows:

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

This is a small script, so let's explain it line by row:

1 We first create an object reference to the UserAccounts.CommonDialog object (named "Objdialog").

2 Next, we set the Filter property of the dialog box. We're going to show all the files, so we set the filter to this:
Objdialog.filter = "All Files|*.*"

What if we just want to display a text file? In this case, we will use the following filter:
Objdialog.filter = "Text files|*.txt"

You might be able to see how it works: We provide a description of the file type (Text files), then insert a vertical bar separator (|), and then use the standard wildcard character to indicate all the. txt files (*.txt).
Do you want to display the. txt file by default, and then provide the user with the option to view all files? Then you can use the following code:
Objdialog.filter = "Text files|*.txt| All Files|*.* "

Give it a try and you'll see what we mean.

3) Then, we specify the default folder.
By default, we want the dialog box to display the files located in the root folder of drive C, so we set the "Initialdir" property:
Objdialog.initialdir = "C:\"

Do you want to display the files in the C:\Windows folder? Then you can use the following code:
Objdialog.initialdir = "C:\Windows"

Don't worry: This is a real "File open" dialog box, so you can click and stop at any time. Starting from C:\Windows does not mean that you can only open files in that folder.

4 Finally, we use the following line of code to display the dialog box:

Intresult = Objdialog.showopen

Now we just sit down and wait for the user to select the file and click OK (or wait for the user to click Cancel).
If the user clicks Cancel, the variable intresult is set to 0. In our script, we check the value of the intresult, and if it is 0, we will only need to use Wscript.Quit to terminate the script.
But what if the user actually chooses the file and clicks OK? In this case, the Intresult will be set to-1, and the "FileDialog" property will be set to the pathname of the selected file. Our script only echoes the pathname, which means we'll get output similar to the following:

C:\WINDOWS\Prairie wind.bmp

Needless to say, you are not limited to simply echoing the file path. In fact, you can use WMI, FileSystemObject, or some other technology to bind the file, and then perform actions such as deleting, copying, compressing, or retrieving file properties-you can do almost anything that the file can do.

But anyway, you need to use a script.

By the way, using this method, you can select only one file at a time, and you cannot hold down the CTRL key to select multiple files. There is a way to select multiple files, at least on an XP computer, but we can only leave this issue for discussion in a later column.

Display the Select Folder dialog box?

Q: Hey, Scripting Guys! In the previous column, you showed us how to show a dialog box to the user so they can select a file. Is there a way for users to display a dialog box that only allows them to select a folder?

answer: hello.

In fact, we have the BrowseForFolder method, which is part of the Windows Shell object.

Let me give you a sample of the script and then talk about how it works:

   

Copy Code code as follows:

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

First, we define a pair of constants: Window_handle and OPTIONS.
The Window_handle constant indicates the numeric ID that needs to be specified for the dialog box to display, and for the script, this value should always be 0.
Setting the options to 0 means that we will display a very simple dialog box, a dialog box that restricts users from selecting only from a Folder list. Alternatively, we can set the OPTIONS to &H10&. In this case, our dialog box will include the text area where the user can type the folder path.

After defining the constants, we create an instance of the Shell.Application object and then use the following code to display the Browse for Folder dialog box:
Set Objfolder = Objshell.browseforfolder _
(Window_handle, "Select a folder:", Options, "C:\")

As you can see, we just call the BrowseForFolder method and pass four parameters:
Window_handle, as we have indicated, is the numeric ID assigned to the dialog window.
Text string Select A folder:, as a descriptive message that appears in the dialog box.
Options, which represent constants that construct the choices used by the dialog box.
C:\, as the root folder of the dialog box. The dialog box opens C:\, but does not allow you to select the top file location in the tree view (for example, you cannot select My Computer). If the root folder is set to C:\Scripts, only the user is allowed to select the folder C:\Scripts and all its subfolders.

The code produces a dialog box similar to the one displayed on the screen.
(Do you have any questions, yes, you have seen this dialog box before.) Many Windows applications use the same method, the same dialog box. )

At this point, our script pauses, waits for the user to select a folder and clicks OK, or clicks Cancel. When the user does one of these two actions, the dialog box is cleared and the action is stored in the object reference objfolder.

So how do we know if the user has selected a folder and clicked OK, or just clicked Cancel? The following code block is used to solve this problem:
If Objfolder is nothing Then
Wscript.Quit
End If

The code checks whether our object reference (objfolder) equals a real object (this is the purpose of the keyword Nothing). If Objfolder equals nothing, it means that the user clicks Cancel; In that case, we simply exit the script with Wscript.Quit. If objfolder is not equal to nothing, then objfolder must point to a real object, and the script will continue to run.

Because of the characteristics of the Shell object, the following two lines of code are necessary:
Set objFolderItem = objfolder.self
Objpath = Objfolderitem.path

When the user selects a folder and clicks OK, they will get an instance of the Shell folder object. However, for some reason you cannot use the Shell folder object, and if we want to retrieve the path to the selected folder, we need to replace it with a FolderItem object. (Why is that so?) We don't know. So our first line of code uses the Self method to return a FolderItem object that is the same as our Folder object. The second line of code stores the path to this FolderItem object in the variable Objpath. It looks a little clumsy, but it does work.

Finally we echo back to the path of the selected folder, to which the work has been completed.

As we explained, the example dialog box uses the C:\ As the root folder, you are not allowed to select a folder that is located elsewhere on your computer. Sometimes it's good to do this, forcing users to choose from a specific set of folders. Sometimes, however, you want to enable users to select a folder anywhere in the file system. Is that possible?

Of course. We do not dwell on this modified script, but the script will set my computer as the root folder:
    

Copy Code code as follows:

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 would expect, the resulting dialog box offers a lot of choices. That's exactly what you need.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.