There are four kinds of variables in batch files: command line arguments, environment variables, ERRORLEVELs, and FOR variables. All but ERRORLEVEL are of type "string"; ERRORLEVELs are "byte" type variables.

Command Line Arguments

Command line arguments are strings that follow the name of the batch file on the command-line when the batch file is executed. To reference a command-line argument in a batch file, use the percent sign % with a digit from 0 to 9. The variable %0 holds the name of the batch file that is being run, %1 holds the first argument following the name, %2 holds the second argument and so on.

A delimiter in a batch file is a space, a tab, a comma, a semicolon, or an equal sign. When the command-line interpreter parses the list of command line arguments and encounters a delimiter, the two strings that the delimiter separates are considered to be different arguments. For example, the following batch file prints out the first and second command-line arguments:

rem batfileI.bat 
@echo off 
echo %1 
echo %2
and if batfileI.bat is executed like so:
 
C:\>batfileI this=that 
then the following output will be produced:
this 
that

Note that:

Environment Variables

Like csh and sh, Windows has a set of predefined environment variables that may be used when writing batch files. Some common Windows environment variables and the values that they contain are:

The value of any environment variable may be changed using the set command, which will be seen later. However, remember that the command-line interpreter uses these variables to control its own operations. These variables are reserved by the command-line interpreter and, to avoid problems, they should not be used for purposes other than the ones for which they are intended.

ERRORLEVEL

The ERRORLEVEL variable holds the exit code of the last executable program invoked, whether from running a batch file, or from before the batch file was invoked. ERRORLEVEL remains set until another executable program is run by the command-line interpreter. If the last executable program run does not return any exit code, the command-line interpreter sets ERRORLEVEL to zero. The only action that may be undertaken with ERRORLEVEL is to compare it to a number:

 
ERRORLEVEL number 
where number is any number. This form returns true if the last executable returned an exit code that is equal to or greater than number; otherwise, false is returned.

Since ERRORLEVEL is of type "byte", its value ranges from 0 to 255. Some common values and their definitions are:

The expression ERRORLEVEL number will be used in an if statement in the control structures section.

FOR variables

FOR variables are used only in a for statement. Inside batch files, FOR variables are always in the form of a double percent sign [%%] followed by a single letter. The following script contains a simple for statement that prints out all elements in the list "1 2 3 4" in turn:

rem batfileII.bat 
for %%a in (1 2 3 4) do echo %%a 
The output is:
C:\>batfileII 
1
2
3
4

The variable %%a is a FOR variable. It temporarily holds each element in the list "1 2 3 4", and thus allows the echo command to access these elements. Do not worry about the details of the for statement now, since it will be fully discussed in the section about control structures. Remember, a FOR variable can be used in a for statement only; FOR variables have no meaning if used anywhere else.

The set Command

set displays a list of variables defined in the current environment and their values. The set command may also be used to create a variable, or change the value of an already-existing variable. This command has the form:

set [name=[value]]
where name is the name of the variable and value is the value assigned to it.

It is not necessary for set to take any argument. If set is typed at the prompt, then a list of variables defined in the current environment is displayed. If a value is not supplied for the variable (i.e., by typing C:\>set name), then the current setting for that variable will be deleted from the environment. If the full form C:\>set name=value is supplied, then name is created (if it does not already exist) and is set to value.

Do not leave any space between name and the equal sign, OR the equal sign and value. If a space is included, Windows will interpret the space as part of the variable name or the value.

To reference a variable, use the following form:

%name%

When the command-line interpreter encounters a pair of percentage sign % enclosing a variable, it replaces the form with the value of the variable.