In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to implement a script that keeps the command window open after vbs runs the command-line tool. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Q:
How do I keep the command window open after running a tool like Ping or Ipconfig?
A:
This question reminds us of the past. When a scripting expert first came to Microsoft, many people thought that WMI and ADSI were too difficult for script writers to use. Therefore, it is recommended that the scripting expert not use WMI or ADSI, but simply use VBScript as a way to invoke command-line tools. In fact, the first chapter written by this scripting expert was a chapter on event log management, which later became part of the Microsoft Windows 2000 scripting guide. It is also a chapter that does not contain any content such as scripting code.
You're right: only scripting experts will find themselves writing a scripting guide that contains absolutely no scripting code.
Of course, script experts using eloquent persuasion, coupled with a large number of pleas, can eventually convince people that it is possible if something called the Microsoft Windows 2000 script Guide actually contains one or two scripts. In the process, however, scripting experts also know a little about invoking command-line tools within scripts, which is why we can answer your questions.
We suspect that you have a script similar to the following that 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: a command window pops up and Ipconfig starts running. The only problem is that the command window closes before you can read the information returned by IPconfig (unless you read it really fast). This is indeed a problem.
So how to solve it? The methods are 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 an instance of the Wscript.Shell object, and then call the Run method to actually run the command-line tool. The difference lies in how the command-line tool is invoked. In the original script, we just called the tool itself:
ObjShell.Run ("ipconfig / all")
This time, the syntax we use is very 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). Then why not call Cmd.exe directly? Well, suppose 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 command windows are available no matter what 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 parameters to that instance. The first of these parameters is / k, which instructs the command shell to do what we ask it to do, and then keep it open. We know that k is the abbreviation of keep, as in "keep open", but we are not sure whether it is true. We can also use the parameter / c (c stands for close), which automatically closes the command window when it completes its task.
What is the purpose of 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 does this and ensures that the command window remains open after that:
Set objShell = CreateObject ("Wscript.Shell")
ObjShell.Run ("% comspec% / k ping 192.168.1.1")
Do you want to run Net.exe to get a 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've got the gist.
If you tend to use a lot of command-line tools in your scripts (there's nothing wrong with this; feel free to use what you think is the most convenient / best tool), you may be interested in this column. it shows you how to change the title and color of the command window. After all these years, scripting experts still have a soft spot for calling command-line tools, and it's still not clear why people think it's a good idea to write scripting guidelines that don't contain any scripts at all.
Thank you for reading! This is the end of this article on "how to implement a script that keeps the command window open after running a command line tool in vbs". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.