Use of the Run () and EXEC () functions of the Wscript.Shell object

Source: Internet
Author: User
Tags chr

The run () and EXEC () functions of the Wscript.Shell object are detailed using http://blog.sina.com.cn/s/blog_6e14a2050102v47g.html

Use of the run and exec functions of Wscript.Shell objects in VbScript

function declaration:

Set Ws = CreateObject ("Wscript.Shell")
Function Exec (ByVal Command as String) as Wshexec
Function Run (ByVal Command as String, [ByVal WindowStyle], [ByVal Waitonreturn]) as Integer

function differences:

1, the return value is different
The return value of Run is an integer, which is 0 or 1 success and failure two states, and the return value of the Exec method is an object, from which you can get console output information and console error messages, such as StdOut and STDERR properties.
Such as:
Dim oshell,exers
Set Oshell = CreateObject ("Wscript.Shell")
CommandLine = "Xcopy.exe"
Set exers = oshell.exec (commandLine)
ErrMsg = ExeRs.StdErr.ReadAll ()
Stdmsg = ExeRs.StdOut.ReadAll ()
WScript.Echo "ErrMsg:" & ErrMsg & "Stdmsg:" & stdmsg
ret = Oshell.run (commandLine, 0, True)
WScript.Echo "Run Method return Value:" & RET
Set Oshell = Nothing
Set exers = Nothing
You can take the console error and console information via the control console.

PostScript: The Wshexec class has a property exitcode,processid,status,stderr,stdin,stdout and a function Terminate, which are well understood.
The Status property has a value of three:
Const wshfailed = 2
Const wshfinished = 1
Const wshrunning = 0

2, execution parameters
The last two arguments of Run, one is the style of the cmd window, and the other is whether to wait for execution to complete.
The last parameter is useful if you want to wait for the execution of this cmd after the end of the program, then execute the statement after run, set this parameter to true, otherwise the statement will not wait for the cmd window to complete, run directly.
In addition, if you use the Exec method, if you want to wait for the CMD in the program after the execution, and then execute the following statement, you can also be done by:
OExec.StdErr.ReadAll () or OExec.StdOut.ReadAll ()
The truth should also be good understanding, to get the output of information, it must be executed after the CMD will be output.

Parameters of the Ws.run function:

Set Ws = CreateObject ("Wscript.Shell")
Ws.run "notepad.exe",
The run function has three parameters:
The first parameter is the path to the program you want to execute, and if there are spaces in the path, remember to enclose it.
such as "" "C:\Program files\nn.exe" "Form or Chr &" C:\Program Files\nn.exe "&CHR (34).
The second parameter is the form of a window, 0 is run in the background, 1 means normal operation, 2 means activating the program and is displayed as minimized, 3 means activating the program and showing it as maximized; a total of 10 such parameters are as follows.
The third argument is whether the script waits or resumes execution, and if set to true, the script waits for the calling program to exit and then execute backwards.
In fact, run as a function, preceded by a variable to accept the return value, in general, if the return is 0, indicating successful execution, if not 0, then the return value is the error code, you can find the corresponding error code.

Window Style Description
0 hides a window and activates another window.
1 Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. The application should specify this flag the first time the window is displayed.
2 Activates the window and displays it as a minimized window.
3 Activates the window and displays it as a maximized window.
4 Displays the window at the nearest window size and position. The active window remains active.
5 Activates the window and displays it at its current size and position.
6 minimizes the specified window and activates the Next top window in Z-order.
7 Displays the window as a minimized window. The active window remains active.
8 Displays the window as the current state. The active window remains active.
9 activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. When you restore a minimized window, the application should specify this flag.
10 sets the display state according to the program state of the startup application.

Details of the parameter () can be found in MSDN:
HTTP://MSDN.MICROSOFT.COM/ZH-CN/LIBRARY/ATEYTK4A (en-us,vs.85). aspx
Http://msdn.microsoft.com/zh-cn/library/d5fk67ky (en-us,vs.85). aspx

Note: After experimental verification, execute the General EXE, it is recommended to use the Exec ()

This article is from the "Qytag (upspringing)" blog, so be sure to keep this source http://qytag.blog.51cto.com/6125308/1568375

Use of the Run () and EXEC () functions of the Wscript.Shell object

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.