Batch File Parameters

<< Click to Display Table of Contents >>

Navigation:  TCC > Aliases & Batch Files > Batch Files >

Batch File Parameters

Like aliases, user-defined functions and application programs, batch files can examine the command line that is used to invoke them. The command tail (everything on the command line after the batch file or alias name) is separated into individual positional parameters (also called parameters or batch variables) by scanning for the spaces, tabs, commas, and equals signs (=) that separate them. (The = separator can be disabled by setting the "CMDBatchDelimiters=No" directive in your TCMD.INI.) For aliases and functions, a forward slash (/) triggers the beginning of a new parameter, e.g. the string xyz/abc is separated into parameters foo and /abc.


These parameters are numbered from %1 to %4095. %1 refers to the first parameter on the command line, %2 to the second, and so on. It is up to the batch file to determine the meaning of each parameter. You can use double quotes to pass spaces, tabs, commas, and other special characters in a batch file parameter; see Parameter Quoting for details.


Parameters that are referred to in a batch file, but which are missing on the command line, appear as empty strings inside the batch file. For example, if you start a batch file and put two parameters on the command line, any reference in the batch file to %3, or any higher-numbered parameter, will be interpreted as an empty string.


A batch file can use the special parameters shown in the table below:





the name of the batch file as entered on the command line


the number of command line parameters, modified by SHIFT


the command tail starting with parameter number n, modified by SHIFT


the command tail from parameter 1 to n - 1


the complete command tail, modified by SHIFT


the complete command tail, unmodified by SHIFT


For example, %3$ means the third and all subsequent parameters. The values of %#, %n$, %-n$, and %$ will change if you use the SHIFT command. To emulate CMD, SHIFT does not affect the value of %*.


For example, if your batch file interprets the first parameter as a subdirectory name then the following line would move to the specified directory:


cd %1


A friendlier batch file would check to make sure the directory exists and take some special action if it doesn't:


iff isdir %1 then

  cd %1


  echo Subdirectory %1 does not exist!




(See the IF and IFF commands.)


Batch files can also use environment variables, internal variables, and variable functions.


Batch file parameters may also use the special CMD compatibility syntax.