In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Detailed explanation of the function of run and exec of WScript.Shell object 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 returned values are different
The return value of Run is an integer, which is the success and failure states of 0 or 1, while the return value of the exec method is an object, from which you can get console output information and console error information, 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 access console errors and console messages.
Note: the WshExec class has an attribute ExitCode,ProcessID,Status,StdErr,StdIn,StdOut and a function Terminate, which are easy to understand.
The Status property has three values:
Const WshFailed = 2
Const WshFinished = 1
Const WshRunning = 0
2, execution parameters
The last two parameters of Run, one is the style of the cmd window, and the other is whether to wait for the execution to complete.
The last parameter is very useful, if you want to wait for the end of the cmd execution of the program, then execute the statement after run, set this parameter to true, otherwise the later statement will not wait for the completion of the cmd window, run directly.
In addition, if you want to wait for the execution of the program in cmd before executing the following statements when you use the exec method, you can also use the following method:
OExec.StdErr.ReadAll () or oExec.StdOut.ReadAll ()
Reason should also be easy to understand, in order to get the output information, there must be cmd implementation before there will be output.
The parameters of the Ws.Run function:
Set Ws = CreateObject ("Wscript.Shell")
Ws.run "notepad.exe"
The run function takes three parameters:
The first parameter is the path of the program you want to execute. If there is a space in the path, remember to enclose it with "".
Such as "C:\ Program Files\ nn.exe" or Chr (34) & "C:\ Program Files\ nn.exe" & Chr (34).
The second parameter is in the form of a window, where 0 runs in the background; 1 indicates that the program is running normally; 2 indicates that the program is activated and displayed as minimized; 3 indicates that the program is activated and displayed as maximized; there are a total of 10 such parameters as shown in the table below.
The third parameter indicates whether the script will wait or continue to execute. If set to true, the script will wait for the calling program to exit before executing backwards.
In fact, as a function, run is preceded by a variable that accepts the return value. Generally speaking, if the return value is 0, it means successful execution. If it is not 0, then the return value is the error code, and the corresponding error can be found through this code.
Window style description
0 hides one window and activates another.
1 activate and display the window. If the window is minimized or maximized, the system restores it to its original size and position. The first time the window is displayed, the application should specify this flag.
2 activate the window and display it as a minimized window.
3 activate the window and display it as a maximized window.
4 display the window according to the nearest window size and position. The active window remains active.
5 activate the window and display it at its current size and position.
6 minimize the specified window and activate 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 its current state. The active window remains active.
9 activate and display 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 when restoring the minimized window.
10 sets the display status according to the program status of the startup application.
For more information on the parameters, please see 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, the implementation of general exe, it is recommended to use Exec ()
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.