Script that keeps the command window open after the VBS runs the command line tool _vbs

Source: Internet
Author: User
Ask:
Hello, Scripting Guy! How do I keep a command window open after I run a tool such as Ping or Ipconfig?
--DB
For:
Hello, DB. This question reminds us of the past. One of the Scripting Guys just came to Microsoft, when many thought WMI and ADSI were too hard for scripting people to use. Therefore, it is recommended that the Scripting Guy not use WMI or ADSI, but simply use VBScript as a way to invoke command-line tools. In fact, the first chapter of the Scripting Guys is a section on event log management, which later became part of the Microsoft Windows 2000 Scripting Guide. It's also a chapter that doesn't include any scripting code.
You're right: only the Scripting Guys will find themselves writing a scripting guide that has absolutely no scripting code.
Of course, the Scripting Guys use eloquent persuasion, with a lot of begging, to finally convince people that it's OK if the thing called the Microsoft Windows 2000 Scripting Guide actually contains one or two scripts. However, in this process, the Scripting Guys also have a little knowledge of invoking command-line tools within the script, which is why we can answer your question.
We speculate that 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: Pop up the command window and Ipconfig start running. The only problem is that before you can read the information returned by IPconfig, the command window closes (unless you read it really fast). This is really a problem.
So how do you solve it? The method is as follows:
Set Objshell = CreateObject ("Wscript.Shell")
Objshell.run ("%comspec%/k Ipconfig/all")
As you can see, this revised script retains the same basic structure as the original script: we create an instance of the Wscript.Shell object, and then call the Run method to actually run the command-line tool. The difference is in how the command-line tool is invoked. In the original script, we just invoke the tool itself:
Objshell.run ("Ipconfig/all")
This time, the syntax we use is vastly different:
Objshell.run ("%comspec%/k Ipconfig/all")
The environment variable%comspec% represents the Windows command shell, which is equivalent to calling Cmd.exe (which of course opens a command window). So why not call Cmd.exe directly? Yes, let's say that your computer is running Windows 98. On these computers, the command shell is invoked by running Command.com because there is no Cmd.exe. Using%comspec% helps ensure that the command window is available regardless of which version of Windows the script is running on.
In other words, instead of running Ipconfig directly when using this script, we run an instance of the command shell and pass several arguments to the instance. The first of these parameters is/k, which instructs the command shell to perform the task we asked it to do, and then leave it open. (We know that K is a shorthand for keep, as in "keep open", but we are not sure if it is true.) We can also use the parameter/C (for close), which closes the command window automatically when the command window finishes the task.
What are the other parameters passed to the command shell? Those parameters are just the commands needed to run IPconfig: Ipconfig/all. Do you want to use Ping.exe to Ping the IP address 192.168.1.1? The following script performs this operation and makes sure that the command window remains open:
Set Objshell = CreateObject ("Wscript.Shell")
Objshell.run ("%comspec%/k ping 192.168.1.1")
Would you like to run Net.exe to get a list of local user accounts and see them later? Ok no problem:
Set Objshell = CreateObject ("Wscript.Shell")
Objshell.run ("%comspec%/k net User")
Want to ¨c well, you've grasped the gist.
If you tend to use command-line tools heavily in your scripts (which is fine with you), you might be interested in this column, which will show you how to change the command window caption and the command window color. Over the years, the Scripting Guys have a good impression of calling command-line tool scripts, and still can't figure out why people think writing a scripting guide that doesn't contain any script is a nice idea.

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.