[Toc][Index]

Batch File Parameters


Like aliases 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 name) is separated into individual 
parameters (also called arguments or batch variables) by scanning for the 
spaces, tabs, and commas that separate the parameters.  A batch file can 
work with the individual parameters or with the command tail as a whole. 
These parameters are numbered from %1 to %127.  %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 
quotation marks to pass spaces, tabs, commas, and other special characters 
in a batch file parameter; see Argument 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 also work with three special parameters:  %0 contains the 
name of the batch file as it was entered on the command line, %# contains 
the number of command line arguments, and %n$ contains the complete 
command-line tail starting with argument number "n" (for example, %3$ 
means the third parameter and all those after it).  The default value of 
"n" is 1, so %$ contains the entire command tail.  The values of these 
special parameters will change if you use the SHIFT command. 
By default, 4DOS uses an ampersand [&] instead of a dollar sign [$] to 
indicate the remainder of the command tail.  For example, %& means all the 
parameters, and %2& means the second parameter and all those after it.  If 
you want to share batch files or aliases between 4DOS and CMD.EXE, see 
Special Character Compatibility for information on selecting compatible 
parameter characters for all products. 
For example, if your batch file interprets the first argument 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
        else & echo Subdirectory %1 does not exist! & quit
        endiff

(see the IF and IFF commands). 
Batch files can also use environment variables, internal variables, and 
variable functions. 

Created using Inf-PHP v.2 (c) 2003 Yuri Prokushev
Created using Inf-HTML v.0.9b (c) 1995 Peter Childs