.Exec - VBScript


.Exec

Overview

.Exec executes a program or command in a new process. It is most frequently used in automating tasks such as launching applications, running batch files, or communicating with external programs.

Syntax

.Exec arglist [waitOnReturn] [errorNumber]

Arguments

  • arglist: Required. Specifies the program or command to execute, followed by its arguments.
  • waitOnReturn (Optional): Boolean. If set to true, the script will wait for the program to finish execution before continuing. Default is false.
  • errorNumber (Optional): Boolean. If set to true, the error code of the executed program will be returned in the Err object. Default is false.

Options/Flags

None.

Examples

  • Launch a program:
    .Exec "C:\Program Files\MyProgram\myprogram.exe"
    
  • Run a command:
    .Exec "echo Hello, world!"
    
  • Wait for program to finish:
    .Exec "C:\Program Files\MyProgram\myprogram.exe", true
    
  • Capture error code:
    .Exec "notepad.exe", false, true
    Err.Number
    

Common Issues

  • Access denied: Ensure the script has sufficient permissions to execute the program.
  • Command not found: Verify that the program or command path is correct.
  • Syntax error: Check the command syntax and ensure all arguments are specified correctly.

Integration

.Exec can be used with other commands to automate complex tasks:

  • .Output to capture the output of the executed program.
  • .Input to provide input to the program.
  • Conditional statements to control the flow of execution based on the program’s return code.

Related Commands

  • .CreateObject
  • .Run
  • .Shell