Thursday, February 1, 2007

Batch Commands: Command Line Parameters

Basic Batch File Command Line Parameters

  • %0 is the program name as it is called
  • %1...%9 are the command line parameters
  • Use SHIFT to shift parameters over, i.e. %1 will get the value in %2.
  • Use SHIFT /n to keep the first n parameters in place while shifting the rest over.  %0 is included, i.e. SHIFT /3 keeps %0...%2 the unchanged.
To iterate each parameter:

    FOR %%a IN (%*) DO (
        REM %%a will have the value of each parameter here.

If the value of the parameter contains quotes, use IF '%1'=='' or IF "%~1"=="".

These delimiters: comma, semicolon, equal, tab, multiple spaces, and first forward slash are replaced by a single space.

A Good Example:

GOTO:%~1 2>NUL
ECHO Invalid argument: %1
ECHO Usage: %~n0 number
ECHO Where: number may be 1, 2 or 3 only

REM Preprocess value 1
GOTO Common

REM Preprocess value 2
GOTO Common

REM Preprocess value 3

REM Common processing of preprocessed values
REM End of batch file

No comments:

Post a Comment