Variables

Environment variables are mainly 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 Ex.ample%

(n.b. Parameter Variables only have one % sign and are always one character long: %A )

Pass a variable from one batch script to another

Where one batch script CALLs another it is recommended that you SETLOCAL in both scripts to prevent any possible naming conflicts, so each script should start with:
@ECHO OFF
SETLOCAL

Then to pass a value back to the original calling script, finish the script with a line like:

ENDLOCAL & SET _output=%_variable%

In the line above %_variable% is a local variable used and visible within just that one batch script
%_output% is an output variable that is passed back to the original calling script

Standard (built-in) Variables

Predefined environment variables
Variable Typical value:
WinXP
Typical value:
Vista/2008
%ALLUSERSPROFILE% C:\Documents and Settings\All Users C:\ProgramData
%APPDATA% C:\Documents and Settings\{username}\AppData\Roaming C:\Users\{username}\AppData\Roaming
%CommonProgramFiles% N/A C:\Program Files\Common Files
%COMPUTERNAME% {computername} {computername}
%COMSPEC% C:\Windows\System32\cmd.exe C:\Windows\System32\cmd.exe
%HOMEDRIVE% C: C:
%HOMEPATH% \Documents and Settings\{username} \Users\{username}
%LOCALAPPDATA%N/A C:\Users\{username}\AppData\Local
%PATH% C:\Windows\System32\;C:\Windows\;C:\Windows\System32\Wbem C:\Windows\System32\;C:\Windows\;C:\Windows\System32\Wbem
%PATHEXT% .COM; .EXE; .BAT; .CMD; .VBS; .VBE; .JS ; .WSF; .WSH; .COM; .EXE; .BAT; .CMD; .VBS; .VBE; .JS ; .WSF; .WSH; .MSC
%ProgramData% N/A C:\ProgramData
%PROGRAMFILES% C:\Program Files C:\Program Files
%ProgramFiles(x86)% 1 C:\Program Files (x86) C:\Program Files (x86)
%PROMPT% $P$G $P$G
%Public% N/A C:\Users\Public
%SYSTEMDRIVE% C: C:
%SYSTEMROOT% C:\Windows C:\Windows
%TEMP% and %TMP% C:\Documents and Settings\{username}\Local Settings\Temp C:\Users\{Username}\AppData\Local\Temp
%USERNAME% {username} {username}
%USERPROFILE% C:\Documents and Settings\{username} C:\Users\{username}
%WINDIR% C:\Windows C:\Windows

1 Only on 64 bit systems, is used to store 32 bit programs.

By default, files stored under Local Settings do not roam with a roaming profile.

%ERRORLEVEL% is a dynamic variable that is automatically set when a program exits.

Dynamic Variables

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: you should not attempt to directly SET any of the dynamic variables above.

"Men may be convinced, but they cannot be pleased against their will. But though taste is obstinate, it is very variable, and time often prevails when arguments have failed" - Samuel Johnson

Related:

CALL - Evaluate environment variables
SET - Display, set, or remove variables
Q100843 - The four types of environment variable
Q286705 - Set compatibility variables
Q242557 - Registry Settings for Folder Redirection
Managing Roaming User Data Deployment Guide - Microsoft.com (Word Doc)



Back to the Top

Simon Sheppard
SS64.com