" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#008000" VLINK="004000" ALINK="#00C000">
SUBSTITUTION PARAMETERS IN A BATCH FILE
A batch file may contain substitution parameters which can be
substituted when executing the batch either for command line parameters or
for the DOS environment variables.
Let us consider, how the substitution parameters can be substituted for
the command line. Suppose there is a batch file A.BAT in the current
directory which containes special symbols:
%1, ... , %n, where n is a digit from 0 to 9.
Here is one of possible command lines calling up the batch file
processing:
a parm1 parm2 parm3 ... parm9 parm10 parm11 ...
where "a" - the batch file name (by default, with .BAT extention). The
rest are the reference parameters which are to substitute the symbols %1
... %n in a batch file A.BAT.
Character string is considered to be a parameter if it is limited on
both sides by some of the following symbols: space, tab symbol, switch
symbol, end of line.
The procedure of substitution is the following: while processing a
batch file line the Shell comes across a special symbol "%". It then looks
at the following symbol and if it is a number from 0 to 9, then these two
symbols (% and number) are substituted for reference parameter value found
in the command line. The name of the batch file is considered to be a zero
reference parameter. The rest of the parameters are numbered according to
their location in the command line. In our example, the substitution pairs
will be:
"%0" will be substituted for "a";
"%1" ...................... "parm1";
"%2" ...................... "parm2";
etc. up to
"%9"....................... "parm9".
Despite the fact that the quantity of reference parameters can be
limited only by the command line length you can address simultaneuosy only
the first ten, including batch file name which is considered to be the zero
parameter. To access the rest of the parameters in batch files use the
SHIFT command. Of course, character strings and the length of each
reference parameter are arbitrary, although the value of the initial
parameter %0 can contain only symbols allowed for file names.
Example:
Suppose, the file JACK.BAT contains following lines:
TASM %1.asm;
TLINK %1.obj,,%2
Suppose you enter the command:
D>jack aaa eee
After substitution the two lines above will be:
TASM aaa.asm;
TLINK aaa.obj,,eee
A batch file also can get information from the DOS environment. For
more details on DOS environment see DOS command SET description.
The procedure of substitution of environment variables in a batch file
command line is the following:
Having found a special symbol "%" in a batch file, the Shell looks for
another "%" symbol or the end of line. All symbols located between these two
special symbols are considered to be a name of a DOS Environment variable.
After that the name, including special symbols, is removed from the batch
file command line and substituted for the value of the DOS Environment
variable. If there are no variables bearing this name in the Environment no
substitution takes place although the initial name is still removed.
There are some restrictions on the names of DOS Environment variables
to be used as substitution parameters:
a) variable name cannot start with a figure (although this is not
stipulated by SET command syntax), because in such a case it would
provoke substitution for a batch file command line reference parameter;
b) the name cannot contain the special symbol "%".
Though, if there are two symbols "%" in a row in a batch file line, the
Shell will transfer one of them to the output command. Doubling of "%"
symbols seems to be the only way to place a single "%" symbol in a command
file.