variable Specifies the environment-variable name. string Specifies a series of characters to assign to the variable.

Type SET without parameters to display the current environment variables.

If Command Extensions are enabled SET changes as follows:

SET command invoked with just a variable name, no equal sign or valuewill display the value of all variables whose prefix matches the namegiven to the SET command. For example:

SET P

would display all variables that begin with the letter 'P'

SET command will set the ERRORLEVEL to 1 if the variable name is notfound in the current environment.

SET command will not allow an equal sign to be part of the name ofa variable.

Two new switches have been added to the SET command:

SET /A expression SET /P variable=[promptString]

The /A switch specifies that the string to the right of the equal signis a numerical expression that is evaluated. The expression evaluatoris pretty simple and supports the following operations, in decreasingorder of precedence:

If you use any of the logical or modulus operators, you will need toenclose the expression string in quotes. Any non-numeric strings in theexpression are treated as environment variable names whose values areconverted to numbers before using them. If an environment variable nameis specified but is not defined in the current environment, then a valueof zero is used. This allows you to do arithmetic with environmentvariable values without having to type all those % signs to get theirvalues. If SET /A is executed from the command line outside of acommand script, then it displays the final value of the expression. Theassignment operator requires an environment variable name to the left ofthe assignment operator. Numeric values are decimal numbers, unlessprefixed by 0x for hexadecimal numbers, and 0 for octal numbers.So 0x12 is the same as 18 is the same as 022. Please note that the octalnotation can be confusing: 08 and 09 are not valid numbers because 8 and9 are not valid octal digits.

The /P switch allows you to set the value of a variable to a line of inputentered by the user. Displays the specified promptString before readingthe line of input. The promptString can be empty.

Environment variable substitution has been enhanced as follows:

%PATH:str1=str2%

would expand the PATH environment variable, substituting each occurrenceof "str1" in the expanded result with "str2". "str2" can be the emptystring to effectively delete all occurrences of "str1" from the expandedoutput. "str1" can begin with an asterisk, in which case it will matcheverything from the beginning of the expanded output to the firstoccurrence of the remaining portion of str1.

May also specify substrings for an expansion.

%PATH:~10,5%

would expand the PATH environment variable, and then use only the 5characters that begin at the 11th (offset 10) character of the expandedresult. If the length is not specified, then it defaults to theremainder of the variable value. If either number (offset or length) isnegative, then the number used is the length of the environment variablevalue added to the offset or length specified.

%PATH:~-10%

would extract the last 10 characters of the PATH variable.

%PATH:~0,-2%

would extract all but the last 2 characters of the PATH variable.

Finally, support for delayed environment variable expansion has beenadded. This support is always disabled by default, but may beenabled/disabled via the /V command line switch to CMD.EXE. See CMD /?

Delayed environment variable expansion is useful for getting aroundthe limitations of the current expansion which happens when a lineof text is read, not when it is executed. The following exampledemonstrates the problem with immediate variable expansion:

set VAR=before if "%VAR%" == "before" ( set VAR=after if "%VAR%" == "after" @echo If you see this, it worked )

would never display the message, since the %VAR% in BOTH IF statementsis substituted when the first IF statement is read, since it logicallyincludes the body of the IF, which is a compound statement. So theIF inside the compound statement is really comparing "before" with"after" which will never be equal. Similarly, the following examplewill not work as expected:

set LIST= for %i in (*) do set LIST=%LIST% %i echo %LIST%

in that it will NOT build up a list of files in the current directory,but instead will just set the LIST variable to the last file found.Again, this is because the %LIST% is expanded just once when theFOR statement is read, and at that time the LIST variable is empty.So the actual FOR loop we are executing is:

for %i in (*) do set LIST= %i

which just keeps setting LIST to the last file found.

Delayed environment variable expansion allows you to use a differentcharacter (the exclamation mark) to expand environment variables atexecution time. If delayed variable expansion is enabled, the aboveexamples could be written as follows to work as intended:

set VAR=before if "%VAR%" == "before" ( set VAR=after if "!VAR!" == "after" @echo If you see this, it worked )

set LIST= for %i in (*) do set LIST=!LIST! %i echo %LIST%

If Command Extensions are enabled, then there are several dynamicenvironment variables that can be expanded but which don't show up inthe list of variables displayed by SET. These variable values arecomputed dynamically each time the value of the variable is expanded.If the user explicitly defines a variable with one of these names, thenthat definition will override the dynamic one described below:

%CD% - expands to the current directory string.

%DATE% - expands to current date using same format as DATE command.

%TIME% - expands to current time using same format as TIME command.

%RANDOM% - expands to a random decimal number between 0 and 32767.

%ERRORLEVEL% - expands to the current ERRORLEVEL value

%CMDEXTVERSION% - expands to the current Command Processor Extensions version number.

%CMDCMDLINE% - expands to the original command line that invoked the Command Processor.

C:\WINDOWS>HELP SETLOCALBegins localization of environment changes in a batch file. Environmentchanges made after SETLOCAL has been issued are local to the batch file.ENDLOCAL must be issued to restore the previous settings. When the endof a batch script is reached, an implied ENDLOCAL is executed for anyoutstanding SETLOCAL commands issued by that batch script.

The SETLOCAL command will set the ERRORLEVEL value if givenan argument. It will be zero if one of the two valid argumentsis given and one otherwise. You can use this in batch scriptsto determine if the extensions are available, using the followingtechnique: