Code of the "select file or folder" dialog box displayed in VBS

Source: Internet
Author: User

1. The "select file" dialog box is displayed.

Q:Hi, Scripting Guy! Is there any way for me to use a script to display a dialog box for users to select files?

A:Hello. |
If you are using Windows 2000, we do not know how to implement this operation. At least this method is not built in the operating system.
However, if you are using Windows XP, the situation is different. On Windows XP, you can use"UserAccounts. CommonDialog"Object to display a standard" file open "dialog box.

You can use a script similar to the following code:

Copy codeThe Code is 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 line:

1) We first create an object reference for the UserAccounts. CommonDialog object (named "objDialog ").

2) then, we set the "filter" attribute in the dialog box. We want to display all the files, so we set the filter to this:
ObjDialog. Filter = "All Files | *.*"

What if we only want to display text files? In this case, we will use the following filters:
ObjDialog. Filter = "Text Files | *. txt"

You may be able to see how it runs: We provide Text Files for the file type, insert a vertical separator (|), and finally use a standard wildcard to indicate all. txt file (*. txt ).
Do you want to display the. txt file by default, and then provide users with the option to view all files? You can use the following code:
ObjDialog. Filter = "Text Files | *. txt | All Files | *.*"

Try it and you will understand what we mean.

3) then, we specify the default folder.
By default, we want the dialog box to display files in the root folder of drive C, so we set the "InitialDir" attribute as follows:
ObjDialog. InitialDir = "C :\"

Do you want to display files in the C: \ Windows folder? You can use the following code:
ObjDialog. InitialDir = "C: \ Windows"

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

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

IntResult = objDialog. ShowOpen

Now, we only need to sit down and wait for the user to select the file and click "OK" (or wait for the user to click "cancel ").
If you click "cancel", the variable intResult is set to 0. In our script, we check the intResult value. If it is 0, we only need to use Wscript. Quit to terminate this script.
But what if the user selects a file and clicks "OK? In this case, intResult is set to-1, and the "FileDialog" attribute is set to the path name of the selected file. Our script only displays the path name, which means we will get output similar to the following:

C: \ WINDOWS \ Prairie Wind.bmp

Needless to say, you are not limited to only displaying the file path. In fact, you can use WMI, FileSystemObject, or some other technology to bind the file, delete, copy, compress, or retrieve file attributes. You can perform almost all operations on a file.

However, you must use the script in any case.

By the way, you can select only one file at a time, rather than holding down the "Ctrl" key to select multiple files. There is one way to select multiple files, at least on the XP computer, but we can only leave this issue to a later column for discussion.

2. The "select folder" dialog box is displayed?

Q:Hi, script expert! In the previous column, you showed us how to display a dialog box for users so that they can select files. Is there a dialog box for users to select folders?

A:Hello.

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

Let me give you a script example and explain how it works:

   

Copy codeThe Code is 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 constant WINDOW_HANDLE indicates the numeric ID that needs to be specified for the dialog box to be displayed. For scripts, this value should always be 0.
Setting OPTIONS to 0 indicates that a very simple dialog box is displayed. A dialog box that limits users to select only from the folder list is displayed. Alternatively, we can set OPTIONS to & H10 &. In this case, our dialog box will include the development area. You can enter the folder path here.

After defining constants, we create an instance of the Shell. Application object, and then use the following code to display the "Browse folder" dialog box:
Set objFolder = objShell. BrowseForFolder _
(WINDOW_HANDLE, "Select a folder:", OPTIONS, "C :\")

As you can see, we only call the BrowseForFolder method and pass four parameters:
WINDOW_HANDLE is the numeric ID assigned to the dialog box window, as we have explained.
Select a folder:, a text string that serves as a descriptive message displayed in the dialog box.
The constant of the OPTIONS used to construct the dialog box.
C: \, used as the root folder of the dialog box. The dialog box opens C: \, but you are not allowed to select the file location above the tree view (for example, you cannot select "my computer "). If you set the root folder to C: \ Scripts, only the C: \ Scripts folder and all its subfolders can be selected.

This code will generate a dialog box similar to that displayed on the screen.
(If you have any questions, yes, you have seen this dialog box before. Many Windows applications use the same method, the same dialog box .)

In this case, the script is paused. Wait for the user to select a folder and click OK, or click Cancel. When you perform one of these two operations, the dialog box will be cleared and the operation will be stored in the object reference objFolder.

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

This code checks whether our object reference (objFolder) is equal to a real object (this is the use of the keyword Nothing ). If objFolder is equal to Nothing, it means that the user has clicked cancel. If so, we simply use Wscript. Quit to exit the script. If objFolder is not the same as Nothing, objFolder must point to a real object, so the script will continue to run.

Because of the characteristics of Shell objects, the following two lines of code are necessary:
Set objFolderItem = objFolder. Self
ObjPath = objFolderItem. Path

When you select a Folder and click OK, they will get an instance of the Shell Folder object. However, you cannot use the Shell Folder object for some reason. To retrieve the path of the selected Folder, we must use the FolderItem object instead. (Why? We don't know .) Therefore, our first line of code uses the Self method to return a FolderItem object, which is the same as our Folder object. The second line of code stores the path of this FolderItem object in the variable objPath. It looks awkward, but it does work.

Finally, the path of the selected folder is displayed. This operation has been completed.

The example dialog box uses C: \ as the root folder. You are not allowed to select a folder located elsewhere on your computer. Sometimes this works fine; this forces users to select from a specific folder. However, you may want to select folders in any location in the file system. Is this possible?

Of course. We will not detail this modified script, but it will set "my computer" as the root folder:
    

Copy codeThe Code is 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 expected, the displayed dialog box provides many options. This is 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.