Home NT NT Syntax
Windows NT Syntax : Variables

Environment variables are often used within batch files, they can be created, modified and deleted using the SET command.

Variables can be displayed using either SET or ECHO.

Variables have a percent sign on both sides: %ThisIsAVariable%
The variable name can include spaces, punctuation and mixed case: %Another Example%

Parameter Variables only have one % sign and are always one character long: %A

Pass a variable from one script to another
Where one batch script CALLs another it is recommended that you SETLOCAL in both scripts to prevent possible naming conflicts, so each script should start with:

@ECHO OFF
SETLOCAL

or
@ECHO OFF & SETLOCAL

To pass a value back to the original calling script use the syntax:

ENDLOCAL & SET v_output=%v_variable%

%v_variable% is a variable used throughout the batch script
%v_output% is an output variable passed back to the calling script

Standard Variables

Some Environment Variables such as %ERRORLEVEL% are automatically set at installation or during login. (See Q100843 for details)

Under Windows 2000 there are several dynamic environment variables that can be expanded but which don't show up in the list of variables displayed by SET.
These are computed each time the variable is expanded.

%CD% - expands to the current directory string.

%DATE% - expands to current date using same region specific 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.

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

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

Note: if you explicitly define a variable with one of these names, then that definition will override the dynamic one described above.

Related commands:

CALL - Evaluate environment variables
SET - Display, set, or remove variables
Q286705 - Set compatibility variables (XP __COMPAT_LAYER)



Simon Sheppard
SS64.com