In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you the "VBS how to display the selection file or folder dialog box", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "VBS how to display the selection file or folder dialog box" this article.
1. Display the dialog box of "Select File"
Q: hi, Scripting Guy! Is there any way for me to use a script to show the user a dialog box for the user to select a file to use?
A: Hello. | |
If you are using Windows 2000, we don't know how to do this, at least there is no built-in method 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 similar to the following:
The 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 (named "objDialog") to the UserAccounts.CommonDialog object.
2) next, we set the filter property of the dialog box. We want to display all the files, so we set the filter like this:
ObjDialog.Filter = "All Files | *. *"
What if we just want to display text files? In this case, we will use the following filter:
ObjDialog.Filter = "Text Files | * .txt"
You may be able to see how it works: we provide a description for the file type (Text Files), then insert a vertical bar separator (|), and finally use standard wildcards to indicate all .txt files (* .txt).
Do you want to display .txt files by default, and then provide users with the option to view all files? Then you can use the following code:
ObjDialog.Filter = "Text Files | * .txt | All Files | *. *"
Have 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 like this:
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 at will and stop at any time. Just because you start with C:\ Windows doesn't mean you can only open files in that folder.
4) finally, we display the dialog box using the following line of code:
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 will be set to 0. In our script, we check the value of intResult, and if it is 0, we will only need to use Wscript.Quit to terminate the script.
But what if the user actually selects the file and clicks OK? In this case, intResult will be set to-1 and the "FileDialog" property will be set to the pathname of the selected file. Our script only returns 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 echoing file paths. In fact, you can bind the file using WMI, FileSystemObject, or some other technology, and then delete, copy, compress, or retrieve file properties on it-you can do almost everything you can with the file.
But in any case, you need to use scripts.
By the way, with this method, you can only select one file at a time, but you cannot press and hold the "Ctrl" key to select multiple files. There is a way to select multiple files, at least on XP computers, but we can only discuss this issue in a future column.
Second, display the "Select folder" dialog box?
Q: hi, script expert! In the previous column, you showed us how to show users a dialog box so that they can select files. Is there a way to show users a dialog box and just let them select a folder?
A: Hello.
In fact, we have the BrowseForFolder method, which is part of the Windows Shell object.
Let me give you an example of a script, and then talk about how it works:
The copy 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 WINDOW_HANDLE constant represents the numeric ID; that needs to be assigned to the dialog box to be displayed. For scripts, this value should always be 0.
Setting OPTIONS to 0 means that we will display a very simple dialog box that restricts users to select only from the list of folders. Alternatively, we can set OPTIONS to & H10 &. In this case, our dialog box will include a text area where users can type a folder path.
After defining the constant, we create an instance of the Shell.Application object, and then display the Browse for folder dialog box using the following code:
Set objFolder = objShell.BrowseForFolder _
(WINDOW_HANDLE, "Select a folder:", OPTIONS, "C:\")
As you can see, we just call the BrowseForFolder method, passing four parameters:
The WINDOW_HANDLE, as we explained, is the digital ID assigned to the dialog window.
The text string Select a folder:, is displayed as a descriptive message in the dialog box.
OPTIONS, which represents the constant of the options used to construct the dialog box.
C:\, will be the root folder of the dialog box. The dialog box opens C:\, but does not allow you to select the file location higher in the tree view (for example, you cannot select "my computer"). If the root folder is set to C:\ Scripts, only the user will be allowed to select the folder C:\ Scripts and all its subfolders.
The code produces a dialog 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 is paused, waiting for the user to select a folder and click OK, or click cancel. When the user does one of these two operations, the dialog box is cleared and the operation is stored in the object reference objFolder.
So how do we know if the user selected a folder and clicked OK, or just clicked cancel? The following code blocks are 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 purpose of the keyword Nothing). If objFolder equals Nothing, it means that the user clicked 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; so the script will continue to run.
Due to 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; if we want to retrieve the path to the selected folder, we have to use the FolderItem object instead. Why is this so? We don't know. So our first line of code uses the Self method to return a FolderItem object that is exactly the same as our Folder object. The second line stores the path to this FolderItem object in the variable objPath. It looks a little clumsy, but it does work.
Finally, we echo the path to the selected folder, where the work is done.
As we explained, the sample dialog box uses C:\ as the root folder, which does not allow you to select folders located elsewhere on your computer. Sometimes this is good; it forces the user to choose from a specific set of folders. Sometimes, however, you want to enable users to select folders anywhere in the file system. Is it possible?
Yes, of course. We will not elaborate on the modified script, but it will set my computer as the root folder:
The copy 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
The above is all the contents of the article "how to display the dialog box for selecting files or folders in VBS". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.