Command - VBScript


Overview

The Command command in VB Script executes a system command, allowing access to various operating system functionalities. It is useful for tasks like file manipulation, process control, and system diagnostics.

Syntax

Command [/?] [/V] [/A:x] [/C:x] [/T:x] [/E:n] [/D:path] [/S] [command] [parameters]

Options/Flags

  • /?: Displays help and usage information.
  • [/V] (optional): Prints a header with the command line used.
  • [/A:x] (optional): Sets the wait status code for executing the command to ‘x’.
  • [/C:x] (optional): Sets the exit status code for executing the command to ‘x’.
  • [/T:x] (optional): Sets the timeout limit for executing the command to ‘x’ milliseconds.
  • [/E:n] (optional): Sets the maximum number of errors allowed before the command fails to ‘n’.
  • [/D:path] (optional): Specifies an alternate working directory for the command.
  • [/S] (optional): Causes the system to search for the command in the system path.

Examples

Example 1: Execute a simple command

Command "dir"

Example 2: Execute a complex command with options

Command "/A:0 /C:1 /E:3 /T:5000 dir /s /b > output.txt"

Common Issues

  • CommandNotFoundError: Ensure the command you are executing is in the system path or specify its full path.
  • TimeoutError: Increase the timeout limit using /T:x if the command takes longer than expected.
  • AccessDeniedError: Verify that the script has sufficient permissions to execute the command.

Integration

Combine the Command command with VB Script’s file system and process management tools for advanced tasks. For example, use Command to execute a command from within a For Each loop or as part of a more complex script automation.

  • Shell: Executes a system command asynchronously.
  • CreateObject(“Wscript.Shell”): Provides advanced control over process creation and system interaction.