Scripts that keep the command window open after vbs runs the command line tool

Source: Internet
Author: User

Q:
Hello, script expert! How can I keep the command window open after running a tool such as Ping or Ipconfig?
-- DB
A:
Hello, DB. This question reminds us of the past. A scripting expert just came to Microsoft, when many people thought that WMI and ADSI were too difficult to use for script writers. Therefore, it is recommended that this scripting expert not Use WMI or ADSI, but simply use VBScript as a method to call the command line tool. In fact, the first chapter written by this scripting expert is a chapter on Event Log Management, which was later part of the Microsoft Windows 2000 script guide. It is also a chapter that does not contain any content such as script writing code.
By the way, only the scripting experts can find that they are writing a script writing guide that does not include any script writing code.
Of course, the scripting Experts use eloquent persuasion, coupled with a large number of pleading, and finally let people believe that if the things called Microsoft Windows 2000 script guide actually contain one or two scripts, yes. However, in this process, the scripting experts also have a slight understanding of calling the command line tool in the script, which is why we can answer your questions.
We guess you have a script similar to the following, which runs the command line tool Ipconfig.exe:
Set objShell = CreateObject ("Wscript. Shell ")
ObjShell. Run ("ipconfig/all ")
You must know that the script is very effective: The Command window pops up and Ipconfig starts to run. The only problem is that the command window is closed before you can read the information returned by IPconfig (unless you read it fast ). This is indeed a problem.
So how to solve it? The method is as follows:
Set objShell = CreateObject ("Wscript. Shell ")
ObjShell. Run ("% comspec %/k ipconfig/all ")
As you can see, the revised script retains the same basic structure as the original script: We create Wscript. an instance of the Shell object, and then call the Run method to actually Run the command line tool. The difference is that you can call this command line tool. In the original script, we only call the tool itself:
ObjShell. Run ("ipconfig/all ")
This time, the syntax we use is quite different:
ObjShell. Run ("% comspec %/k ipconfig/all ")
The environment variable % comspec % Represents the Windows command shell; this is equivalent to calling Cmd.exe (it certainly opens a command window ). Why not call Cmd.exe directly? If your computer runs Windows 98. On these computers, run Command.com to call the command shell because Cmd.exe is not available. Using % comspec % helps ensure that the command window is available no matter what version of Windows the script is running on.
In other words, when using this script, we do not directly run Ipconfig, but the instance that runs the command shell and pass several parameters to the instance. The first of these parameters is/k, which indicates the command shell to execute the tasks that we want it to complete, and then remain open. (We know that k is short for keep, as in "keep open", but we cannot be sure whether it is true or not .) You can also use the/c parameter (c stands for close) to automatically close the command window when the command window completes the task.
What is the role of other parameters passed to the command shell? The parameters are only the commands required to run IPconfig: ipconfig/all. Do you want to use Ping.exe to ping IP address 192.168.1.1? The following script will execute this operation and make sure that the subsequent command window remains open:
Set objShell = CreateObject ("Wscript. Shell ")
ObjShell. Run ("% comspec %/k ping 192.168.1.1 ")
Want to run Net.exe to obtain the list of local user accounts and view them later? Okay, no problem:
Set objShell = CreateObject ("Wscript. Shell ")
ObjShell. Run ("% comspec %/k net user ")
Well, you have understood the purpose.
If you often use a large number of command line tools in the script (this is nothing wrong; please use the most convenient/best tool you think), you may be interested in this column, it shows you how to change the title and color of a command window. After so many years, the script experts still have a liking for calling the script of the command line tool, and still cannot figure out why people think it is a good idea to compile a script writing guide that does not include any script at all.

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.