In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the difference between Run and Exec in VBS. The article is very detailed and has certain reference value. Interested friends must read it!
Set ws = CreateObject("WScript.Shell")'Here creates an object reference for use in the following example code. 'Demon' Note: Why is this variable name so obscene (WS)
syntax: (common sense (Demon note: common sense is my favorite word): when used as a procedure, do not use parentheses, otherwise there will be compiler errors (only or no parameters when parentheses will not error, but it is recommended not to add))
ws.Run(strCommand, [intWindowStyle], [bWaitOnReturn])
[Set objExec =] ws.Exec(strCommand)
These two methods of the WScript.Shell object:
Can be used to run programs, and can take parameters.
Environment variables can be used in the program path.
You can't specify a working directory for your program, you can't set a priority (start can).
To specify a working directory, you can only do so by changing the current working directory of the script host (wscript.exe/cscript.exe): ws.CurrentDirectory = "Working Directory." (Common sense: What is the meaning of the working directory: 1. Some programs need dll and other related files under the corresponding directory to support 2. Relative path problems)
Difference between Run and Exec:
1. Run can run files directly (including protocol files), and will start the associated program to open the file (if there is no association, an error will occur). start has this feature (more advanced, opens the Open With dialog box when there is no association). Exec can only run programs.
ws.Run "c:\boot.ini"ws.Exec "notepad c:\boot.ini"
2. Run can not only run programs directly located in the path environment variable directory, but also run programs "aliases" set in the registry App Paths. Start has this function. Exec does not work, only programs located in the path environment variable directory can be run directly.
ws.Run "iexplore"'iexplore registers aliases in App Paths. ws.Exec "calc"
3. Run can wait for the program to run and then execute the following command. Start has this function. Exec no.
ws.Run "notepad", ,true
4, Exec runs the program path even if it contains spaces, you can also do not quote (parameters if you need quotes, its quotes can not be omitted). Run, start does not have this ability. (Common sense: A quoted character in vbs "itself should be represented by two quotation marks, that is, written as"". You can also use the Chr function to get quotes: chr(34))
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"""
The biggest difference is that Run focuses on startup control (setting window form). (Start is to start, you can also simply set the window to maximize and minimize.) Exec focuses on follow-up control and on controlling command-line programs.
run You can set the running mode (foreground and background: whether to hide the window), window size, activation status (whether to obtain "focus") when the program is running. For specific parameters, please refer to the manual.
Exec can also control the program after it is started: get the running state, get the PID, and force the process to stop. If you are running a command-line program, you can also provide access to the StdIn/StdOut/StdErr stream: write execute commands, get command output, and so on. After running the command line program, commands can only be written through StdIn, and the console window no longer accepts user input.
ws.Run "notepad", 0
'Hide Window
ws.Run "notepad", 4
'Not activated after running, does not disturb the original active window
Note that the manual clearly states that Run does not restrict all programs to run in the window it specifies, and some programs do not listen to it, such as iexplore, calc, etc. When iepore is run, it grabs focus and becomes the active window. Run failed to run calc at minimum.
Set oExec = ws.Exec("mspint")WScript.Echo oExec.ProcessIdoExec.TerminateWScript.Echo oExec.Status '0 for Run, 1 for End Set oExec = ws.Exec("ipconfig")WScript.Echo oExec.StdOut.ReadAll
Applications of Exec:
1, Runas automatic password input: may be designed for security considerations, runas do not receive pipeline transmission or redirection from files, input password must be manually input, this problem troubled many people, but difficult to solve, with Sendkeys is not necessarily safe (Demon Note: I have said many times before, with Sendkeys is unreliable, because it is impossible to guarantee that the target window has always been focused, but often see a lot of people use, really do not know the truth of the masses, sad). If you use the Exec method, you can easily do automatic input.
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 combined use: Exec method can not hide the window, to get the output of the command line program, there will be a black window flashing past, not only ugly, but also let other users mistakenly think it is a Trojan horse, very imperfect. How to solve this problem? Let Exec work with Run!
Set ws = CreateObject("WScript.Shell")host = WScript.FullName'Demon Note: It doesn't have to be so complicated here, LCase(Right(host, 11)) doesn't work If LCase( right(host, len(host)-InStrRev(host,"\") ) = "wscript.exe" Then ws.run "cscript "" & WScript.ScriptFullName & chr(34), 0 WScript.QuitEnd IfSet oexec = ws.Exec ( "ipconfig")Msgbox oExec.StdOut.ReadAll, , "ipconfig"'Do not use WScript.Echo at this time, because the results of running'WScript.Echo on the console will be output on the console, and no dialog box will pop up.
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 representing the appearance of the program window.
Note that not all programs use this information.
bWaitOnReturn
Optional. Boolean indicating whether the script waits to finish executing the program before proceeding to the next statement in the script.
If set to true, the script is not executed until the program finishes executing, 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 an error code) immediately after starting the program.
[Description]
The Run method returns an integer. The Run method starts a program running in a new Windows process.
You can have scripts wait until the program finishes executing before continuing. This allows you to run scripts and programs simultaneously.
Environment variables within the strCommand parameter are automatically expanded.
If a file type is properly registered in a program, the program executes when the Run method is called on files of that type. For example, if Word is installed on your computer system, calling the Run method on a *.doc file starts Word and loads the document.
intWindowStyle Description
0 Hide one window and activate another.
1 Activates and displays 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 Displays windows by nearest window size and position. The active window remains active.
5 Activate the window and display it in its current size and position.
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 in its 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. Applications should specify this flag when restoring minimized windows.
10 Set the display state according to the program state of the startup application.
Example 1
The following VBScript code uses Notepad to open a copy of the currently running script.
Set WshShell = WScript.CreateObject("WScript.Shell")WshShell.Run "%windir%\notepad " & WScript.ScriptFullName
The following VBScript code does the same thing as the above code, except that it specifies the window type, waits for the user to close Notepad, and saves the error code returned from Notepad when Notepad is closed.
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 DIR.
Dim oShellSet 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 parameter is the path to the program you want to execute,
The second parameter is the form of the window, 0 background operation;1 normal operation;2 minimum;3 maximum; default means normal operation
The third parameter indicates whether the script waits or continues. If true, the script waits for the calling program to exit before executing backwards.
Example 1:
Set ws = CreateObject("WScript.Shell")ws.Run "notepad",,Truews.Run "iexplore"
Example 2: Hide the BAT execution window
Set ws = CreateObject("WScript.Shell")ws.Run "x.bat",0
Example 3:
Set ws = CreateObject("WScript.Shell")ws.Run "cmd /c netstat -an>>x.txt",0
Example 4:
Set ws = CreateObject("WScript.Shell")ws.Run "taskkill /f /im iexplore.exe",0
Exec method
Example 1: Running a file
Set ws = CreateObject("WScript.Shell")
ws.Exec "notepad c:/x.txt" 'for Exec should indicate program, this notepad is required
Example 2: Run the program> Get the PID value of the process> Force to end the process (no Run+taskkill strong: if the process is not saved in Notepad, Exec will make an error, but Run will not)> Determine whether the process is running (this function is good)
Set ws = CreateObject("WScript.Shell")Set e = ws.Exec("notepad")MsgBox e.ProcessIde.TerminateWSH.Sleep 1000MsgBox e.Status '0 for Run, 1 for End
Example 3: Get dos command output directly,dos window will flash, Exec has no window control function
Set ws = CreateObject("WScript.Shell")Set e = ws.Exec("ipconfig")MsgBox e.Stdout.ReadAll
Example 4:
Set ws = CreateObject("WScript.Shell")Set e = ws.Exec("cmd /c echo Hi")MsgBox e.Stdout.ReadAll The above is "What is the difference between Run and Exec in VBS" All the contents of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!
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.