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:

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

:1
REM Preprocess value 1
GOTO Common

:2
REM Preprocess value 2
GOTO Common

:3
REM Preprocess value 3

:Common
REM Common processing of preprocessed values
REM End of batch file

No comments:

Post a Comment