Use the VBS implementation to open a specific folder when you start Windows Explorer _vbs

Source: Internet
Author: User
My-script.vbs "C:\Scripts"
Do you have to add double quotes at both ends of the folder path? Not required in this case. However, if there are spaces in the path, double quotes must be added. The following command line will not work:
My-script.vbs C:\Documents and Settings\kmyer
As long as you pass a parameter that contains a space to a script, you must enclose the entire argument in double quotes (otherwise this is not necessary). Other words:
My-script.vbs "C:\Documents and Settings\kmyer"
This is how the command interpreter works.
So, what script are we going to run here? Good question. Finally, a script like this:
Copy Code code as follows:

Set Objshell = CreateObject ("Wscript.Shell")
strpath = wscript.arguments (0)
strpath = "explorer.exe/e," & strpath
Objshell.run strpath

As always, the script does not have much content. We first create an instance of the Wscript.Shell object, which is a Windows script Host object that we use to run the script or executable file in another script. Then we get the first parameter (C:\Scripts) that is provided to the script and store it in a variable named strpath:
strpath = wscript.arguments (0)
There should be no doubt about these, right? Later, we will use the Run method to start Windows Explorer. Before that, however, we should note that the Run method in WSH is essentially the same as the Run dialog box. If you want to use the Run dialog box to start Windows Explorer (focus on the C:\Scripts folder), you need to type the following code:
Explorer.exe/e,c:\scripts
We found that the syntax used above is the same as the syntax we use to start Windows Explorer using the Run method: We only need to build the command and then execute:
strpath = "explorer.exe/e," & strpath
Objshell.run strpath
In line 1th, we take the command explorer.exe/e and attach the folder path (the path is stored in the variable strpath), and then the strpath value will be explorer.exe/e,c:\scripts. On line 2nd, we call the Run method and pass the variable strpath as the command to run. If all goes well (in terms of scripting, things are always going well, right?) , Windows Explorer opens and the focus is positioned on C:\Scripts:

That's cool.
So, what's wrong with that? Nothing, everything is fine. The only drawback to this script is that you need to type the full path of the folder; When you try to open the folder C:\Documents and Settings\Default User\application data\microsoft\ When Systemcertificates\my\certificates, this becomes a problem. But because the system administrator always insists on using the command line, what choice do we have to make?
Well, you can always try GUI methods (don't worry, we won't tell anyone):
Copy Code code as follows:

Const window_handle = 0
Const no_options = 0
Set Objshell = CreateObject ("Shell.Application")
Set Objfolder = Objshell.browseforfolder _
(Window_handle, "Select a folder:", No_options)
Set objFolderItem = objfolder.self
strpath = Objfolderitem.path
Objshell.explore strpath

This script does not require you to do any typing at all. You just start the script, and then it displays the Browse Folder dialog box:

Select the folder and click OK, so that's it.
So, how does this script work? OK, let's first define a pair of constants: Window_handle and No_options. Window_handle is the constant required by the BrowseForFolder method, No_options is just a notification script we want to display the Standard Browse Folder dialog box. After that, we create an instance of the Shell.Application object, and then use the following line of code to display the dialog box:
Set Objfolder = Objshell.browseforfolder _
(Window_handle, "Select a folder:", No_options)
After the dialog box is displayed, it waits until we select a folder and click OK. We then use the following two lines of code to implement: 1 Create an object reference to the selected folder and, 2, store the folder path in the variable strpath:
Set objFolderItem = objfolder.self
strpath = Objfolderitem.path
All we need to do now is invoke the Explore method dedicated to opening Windows Explorer. By passing the variable strpath as a separate parameter, this causes the Windows resource browser to focus on the C:\Scripts when it is opened:
Objshell.explore strpath
What does that mean? This means that you can now open Windows Explorer using a command prompt, or you can use the GUI to open Windows Explorer, which is entirely up to you.
Related Article

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.