Sometimes, there arises a need to launch a program without adding it to TestedApps. This could be, as an example, a DOS command (dir
, del
, copy
, and so on), one of the programs in use, or asynchronous launch of the program without awaiting its completion.
To do just that, we will apply the WScript.Shell
object that is a standard component of the Windows Script Host.
We will consider three possible use cases for the launch: standard program launch, parameterized launch, and execution of the program from the DOS command prompt.
In order to run different commands we need to perform the following steps:
function runExternalCommand() { var calc = ""C:\Program Files\Microsoft Calculator Plus\CalcPlus.exe""; var notepad = "C:\Windows\notepad.exe c:\myfile.txt"; var doscmd = "cmd /c copy c:\myfile.txt c:\myfilecopy.txt"; var ws = Sys.OleObject("WScript.Shell"); ws.Run(calc, SW_SHOWNORMAL); ws.Run(notepad, SW_MINIMIZE); ws.Run(doscmd); }
myfile.txt
file opened; and the file myfile.txt
will be copied to the myfilecopy.txt
.To launch the commands we applied the Run
method of the WScript.Shell
object. The first parameter that is passed to this method is the to-be-executed command per se. The second parameter is the mode of showing the window. We have launched the calculator in the standard mode, and the Notepad in the minimized mode. The second parameter is optional and can be omitted (as shown on the following example).
Please, pay attention to specificity of this method's workings:
cmd
program with the parameter /c
, which is a parameterized command that launches it and completes its session.The Run
method of the WScript.Shell
object can also open files with the help of mapped (associated) applications. For example, opening the myfile.txt
file is also doable as follows:
Sys.OleObject("WScript.Shell").Run("c:\myfile.txt");
Another method to launch programs is through use of WinAPI
. For example, you can launch Notepad using WinAPI
like this:
Win32API.WinExec("C:\Windows\notepad.exe", SW_SHOWNORMAL);
18.191.235.8