Set ws = CreateObject ("Wscript.Shell")
' creates an object reference here for use in the following sample code.
' Demon Note: How can this variable name be so wretched (WS)
Grammar: (Common sense (Demon Note: I like the word common sense): When used as a procedure, do not add parentheses, otherwise a compiler error occurs (the argument is unique or not with parentheses not error, but not recommended))
Copy Code code as follows:
Ws. Run (strcommand, [Intwindowstyle], [Bwaitonreturn])
[Set objexec =] ws. Exec (strcommand)
Wscript.Shell the two methods of the object:
Can be used to run programs, and can take parameters.
You can use environment variables in the program path.
You cannot specify a working directory for the program, and you cannot set a priority (the start command can).
To specify a working directory, you can only change the current working directory of the Script Host (wscript.exe/cscript.exe): Ws.currentdirectory = "working directory." (Common sense: what is the meaning of working directory: 1, some programs need the corresponding directory DLL and other related files support 2, relative path problem)
The difference between run and exec:
1, runs can run the file directly (including the protocol file), will start the associated program to open the file (no association is an error). Start has this feature (more advanced, the Open With dialog box opens when there is no association). exec can only run programs.
Copy Code code as follows:
Ws. Run "C:\Boot.ini"
Ws. Exec "Notepad C:\Boot.ini"
2, run can not only run the program located in the directory of the PATH environment variables, but also run the program "alias" set in the Registry app paths. Start has this feature. exec is not available, only programs that are in the PATH environment variable directory can be run directly.
Copy Code code as follows:
Ws. Run "IEXPLORE"
' IExplore has registered an alias in the App Paths.
Ws. Exec "Calc"
3. Run can wait for the program to complete and then execute the following command. Start has this feature. exec not.
Copy Code code as follows:
4, exec run the program path even if there are spaces, you can not add quotes (parameters such as quotation marks, its quotation marks can not be omitted). Run, start does not have this ability. (Common sense: a quote character in a VBS "itself is expressed in two quotes, written as".) You can also use the CHR function to get quotes: Chr (34))
Copy Code code as follows:
Ws. Exec "C:\Program files\internet Explorer\iexplore. EXE "
Ws. Exec "" "C:\Program files\internet Explorer\iexplore. EXE "" "
Ws. Run "" "C:\Program files\internet Explorer\iexplore. EXE "" "
5, the biggest difference is: Run focus on startup control (Set the window form). (Start listens to the name to be able to start, also can simply set the window to maximize, minimize.) exec focuses on subsequent controls and focuses on controlling command-line programs.
Run can set the running mode of the program (foreground background: whether to hide the window), the window size, the activation state (whether get "focus"), the specific parameters please refer to the manual.
Exec can also control it after starting the program: Get the running state, get the PID, and force the process to abort. If you are running a command-line program, you can also provide access to the Stdin/stdout/stderr stream: Write execution commands, get command output, and so on. After you run the command-line program, you can only write commands through stdin, and the console window no longer accepts user input.
Copy Code code as follows:
Ws. Run "Notepad", 0
' Hide window
Ws. Run "Notepad", 4
' Do not activate after running, do not disturb the original active window
Note that the manual clearly states that run cannot constrain all programs to run in the same window as it specifies, and some programs do not listen to it, such as IExplore, Calc, and so on. When you run Ieplore, it captures the focus as the active window. Run cannot run Calc with minimal.
Copy Code code as follows:
Set oexec = ws. Exec ("MSPaint")
WScript.Echo Oexec.processid
Oexec.terminate
WScript.Echo Oexec.status ' 0 is running, 1 is ending
Set oexec = ws. Exec ("ipconfig")
WScript.Echo OExec.StdOut.ReadAll
EXEC's application:
1, runas automatic password: May be designed for security considerations, RunAs does not receive pipeline transfer or from file redirection, input password must be manually entered, this problem has plagued many people, but difficult to solve, with SendKeys also not necessarily safe (Demon Note: I have said many times before, Use SendKeys is not reliable, because cannot guarantee the target window always obtains the focus, but often sees many people to use, really is unclear the truth the populace, sad. If you use the Exec method, you can easily input automatically.
Copy Code code as follows:
Set ws = CreateObject ("Wscript.Shell")
Set oexec = ws. Exec ("cmd.exe")
Oexec. Stdin.writeline "Runas/user:username Setup.bat"
Oexec. Stdin.writeline "Password"
2, exec and run the combination of use: Exec method can not hide the window, to get the command line program output, there will be a black-whistling window flashed over, not only ugly, but also let other users mistakenly think it is a trojan or something, very imperfect. How to solve this problem? Let exec work with Run!
Copy Code code as follows:
Set ws = CreateObject ("Wscript.Shell")
Host = Wscript.fullname
' Demon Note: It's not so complicated here, LCase (right (host, 11))
If LCase (Right (host, Len (host)-instrrev (host, "\")) = "Wscript.exe" Then
Ws.run "cscript" "" "& Wscript.scriptfullname & Chr (34), 0
Wscript.Quit
End If
Set oexec = ws. Exec ("ipconfig")
Msgbox oExec.StdOut.ReadAll, "ipconfig"
' Do not use WScript.Echo at this time, because it is currently running on the console
' The result of the WScript.Echo will be output in the console and will not pop up the dialog box.
Vbsrun method
Object. Run (strcommand, [Intwindowstyle], [Bwaitonreturn])
Parameters
Object
WshShell object.
Strcommand
A string value representing the command line to run. Includes all parameters to be passed to the executable file.
Intwindowstyle
Optional. An integer value that represents the appearance of the program window.
Please note that not all programs use this information.
Bwaitonreturn
Optional. A Boolean value that indicates whether the script waits to finish executing the program until the next statement in the script continues.
If set to True, the script is executed after the program is executed, and the Run method returns any error codes returned by the program.
If set to False (the default), the Run method automatically returns 0 (not the error code) immediately after the program is started.
Description
The Run method returns an integer. The Run method launches a program that runs in a new Windows process.
You can have the script wait until the program is finished and continue execution. This allows you to run scripts and programs synchronously.
The environment variable within the Strcommand parameter is automatically extended.
If a file type is correctly registered in a program, the program is executed when the Run method is invoked on a file of that type. For example, if you have Word on your computer system, you will start Word and load the document when you call the Run method on the *.doc file.
"Intwindowstyle description"
0 hides a window and activates another window.
1 Activate and display the window. If the window is minimized or maximized, the system restores it to its original size and location. The first time the window is displayed, the application should specify this flag.
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 by 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 the 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 Activate and display the window. If the window is minimized or maximized, the system restores it to its original size and location. When you restore a minimized window, your application should specify this flag.
10 sets the display state according to the program state of the startup application.
"Example 1"
The following VBScript code opens a copy of the currently running script in Notepad.
Copy Code code as follows:
Set WshShell = WScript.CreateObject ("Wscript.Shell")
Wshshell.run "%windir%\notepad" & Wscript.scriptfullname
The following VBScript code is the same as the preceding code, except that it specifies the window type, waits for the user to close Notepad, and saves the error code returned from Notepad when the Notepad is closed.
Copy Code code as follows:
Set WshShell = WScript.CreateObject ("Wscript.Shell")
return = Wshshell.run ("notepad" & Wscript.scriptfullname, 1, true)
"Example 2"
The following VBScript code opens a command window, changes the path to C:\, and executes the DIR command.
Copy Code code as follows:
Dim Oshell
Set Oshell = WScript.CreateObject ("Wscript.Shell")
Oshell.run "cmd/k CD C:\ & Dir"
Set Oshell = Nothing
Run Method:
The Run method has three parameters,
The first argument is the path to the program you want to execute,
The second parameter is the form of the window, 0 running in the background, 1 running normally, 2 minimized, 3 maximized, and the default is normal operation.
The third parameter indicates whether the script is waiting or continuing, and if set to true, the script waits for the calling program to quit and then execute backwards.
Example 1:
Copy Code code as follows:
Set ws = CreateObject ("Wscript.Shell")
Ws. Run "Notepad", True
Ws. Run "IEXPLORE"
Example 2: Hide Bat Execution window
Copy Code code as follows:
Set ws = CreateObject ("Wscript.Shell")
Ws. Run "X.bat", 0
Example 3:
Copy Code code as follows:
Set ws = CreateObject ("Wscript.Shell")
Ws. Run "cmd/c Netstat-an>>x.txt", 0
Example 4:
Copy Code code as follows:
Set ws = CreateObject ("Wscript.Shell")
Ws. Run "taskkill/f/im iexplore.exe", 0
Exec method
Example 1: Running a file
Copy Code code as follows:
Set ws = CreateObject ("Wscript.Shell")
Ws. Exec "notepad c:/x.txt" ' should specify the procedure for exec, this notepad is necessary
Example 2: Run program > Get process PID value > Force End Process (No Run+taskkill: If the process is finished without Notepad, exec will go wrong and run will not) > determine if the process is running (this is a good feature)
Copy Code code as follows:
Set ws = CreateObject ("Wscript.Shell")
Set e = ws. Exec ("notepad")
MsgBox E.processid
E.terminate
WSH. Sleep 1000
MsgBox e.status ' 0 is running, 1 is ending
Example 3: Direct access to DOS command output, DOS window will flash, Exec no window control function
Copy Code code as follows:
Set ws = CreateObject ("Wscript.Shell")
Set e = ws. Exec ("ipconfig")
MsgBox E.stdout.readall
Example 4:
Copy Code code as follows:
Set ws = CreateObject ("Wscript.Shell")
Set e = ws. Exec ("cmd/c echo Hi")
MsgBox E.stdout.readall