Running external programs and DOS commands

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.

Getting ready

On C:\ create the file with the name of myfile.txt.

How to do it...

In order to run different commands we need to perform the following steps:

  1. Create and launch the following function:
    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);
    }
  2. In the result of the function call, the Calculator Plus and Notepad applications will be launched with the myfile.txt file opened; and the file myfile.txt will be copied to the myfilecopy.txt.

How it works...

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:

  • If the path to the file contains spaces, it has to be escaped with double quotes (for example, Calculator Plus instance). The same should be done if the spaces are encountered in the parameters.
  • Parameters are passed in the same line of code together with the path of the launched file.
  • To prompt DOS commands, we apply the cmd program with the parameter /c, which is a parameterized command that launches it and completes its session.

There's more...

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);

See also

  • A good example of running external commands is running an MSI file, which is explained in the Testing installers – running an MSI file recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.69.163