Programming Field

Using environment variables - DOS/Command Prompt Reference

This page describes the essential concept of ‘environment variables’, which can be considered necessary when writing batch programs and similar scripts.

About environment variables

Environment variables are variables that exist within the space (environment) where various programs operate, hence the name ‘environment’. An environment variable is composed of a pair of a ‘name’ and a ‘value’, and there are multiple environment variables within the environment.

Environments are generally created independently for each program executed. Additionally, typically, when one program executes another program, the environment is duplicated, and the executed program belongs to this new environment. Therefore, even if you change the value of an environment variable within a program, those changes will be discarded once the program is terminated. For example, if you change an environment variable within the command prompt, that change will only affect the command prompt and the programs executed from there. When you exit using a command like Exit, the changes will be reverted as if they never occurred.

Depending on the command or program being executed, there are cases where specific environment variables are read, and their values are utilized to determine the behavior of the program. As an example, the Copy command reads an environment variable called ‘COPYCMD’ and processes its value as arguments for the command. Therefore, in batch programs, it is possible to describe the process of ‘setting the value of specific environment variables and then launching a specific program’ (some batch programs are created with this purpose).

Using environment variables

Environment variables can be used by enclosing the variable name with percent signs (%) as follows:

echo %MYVAR%
%SystemRoot%\system32\notepad.exe hoge.txt

Additionally, when setting a value for a variable, the Set command is used.

set MyVar=Hello

The variable name retains the capitalization used when initially set, but the capitalization is not distinguished and is used and managed without case sensitivity.

Please noted that when using ‘%’ to incorporate the value of an environment variable, the value of the environment variable is expanded before executing the command. Please see the examples below for more details.

[Windows NT series] By using Setlocal and Endlocal, it is possible to temporarily duplicate the ‘environment’ and execute commands within that environment. However, during this period, please note that the environment variables in the original environment cannot be modified (they will be discarded at the end of Endlocal or program execution).

[Windows NT series] [Extensions] When using the ‘enabledelayedexpansion’ option with Setlocal, it becomes possible to use ‘!’ instead of ‘%’ for variable expansion. This is used when you want to delay the timing of expanding environment variables, not at the execution time of the entire block of commands, but at the timing of executing each command within the block. For specific details, please refer to the examples mentioned later and the page ‘!’.

Commonly used variables

[Windows NT series] [Extensions] For information about special environment variables provided by Command Prompt, please refer to the ‘%’ page.

TEMP
MS-DOS and later
C:\Temp, %USERPROFILE%\AppData\Local\Temp, etc.
This refers to the location of the ‘temporary directory’. The temporary directory represents the parent directory where temporary files are generated during the processing of programs. Generally, files remaining in this directory can be deleted at any time (as long as they are not in use).
In MS-DOS, manual configuration is required, but some programs utilize this environment variable.
TMP
Windows 95 and later
C:\Temp, %USERPROFILE%\AppData\Local\Temp, etc.
Usually, it contains the same value as ‘TEMP’ (on Windows, the value of ‘TMP’ takes precedence over ‘TEMP’).
PROMPT
MS-DOS and later
$p$g etc.
Specifies the pattern to be displayed on the screen when presenting the ‘prompt’ (waiting for user input). The case (uppercase or lowercase) is not significant. For more details, please see Prompt.
PATH
MS-DOS and later
C:\DOS, C:\Windows\system32;C:\Windows, etc.
Specifies the list of directories to be searched when executing a program/command without specifying the directory name. For more details, please see Path.
COMSPEC
MS-DOS and later
C:\DOS\COMMAND.COM, C:\Windows\system32\cmd.exe, etc.
Specifies the path of the command interpreter. In MS-DOS, there are cases where the command interpreter is reloaded based on the behavior of the executed program, and in such cases, the value of this environment variable is used.
In Windows, this value is set by the system but can also be modified.
windir
Windows 95 and later
C:\Windows etc.
Contains the absolute path of the ‘WINDOWS directory’. This value is set by Windows, and directly modifying the registry where the actual value resides can potentially have adverse effects on the system's operation.
OS
Windows NT series / XP and later
Windows_NT
Contains the string ‘Windows_NT’, indicating that it is part of the ‘Windows NT series’ (including XP, Vista, and later). In Windows 95/98/Me, this variable does not exist, so checking the value of this variable allows for some differentiation of Windows versions.
SystemDrive
Windows NT series / XP and later
C: etc.
It contains the ‘drive where the system is located’. This value is determined during the installation of Windows (it may not necessarily be ‘C:’ depending on the installation destination). The value does not end with ‘\’.
SystemRoot
Windows NT series / XP and later
C:\WINNT, C:\Windows, etc.
Contains the absolute path of the ‘system root directory’. This value is determined during the installation of Windows and is used in many programs and registry settings (for example, ‘%SystemRoot%\system32’ refers to the directory where system programs and DLLs are located).
PATHEXT
Windows NT series / XP and later
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC etc.
Contains the extensions used to search for the actual filename when running a program with a specified name without an extension (semicolon is used as a delimiter, and multiple extensions can be specified). For example, if PATHEXT is set to ‘.COM;.EXE;.BAT’, and the command ‘Test’ is executed, it checks whether the files ‘Test.com’, ‘Test.exe’, and ‘Test.bat’ exist in that order and executes the first file found. While this environment variable can be modified through settings in Windows, it is a system-used variable, so changing it indiscriminately may lead to the inability to execute programs/commands correctly.
HOMEDRIVE
Windows NT series / XP and later
C: etc.
The drive where the user's home directory is created. This value is determined during the installation of Windows (it is not guaranteed to be ‘C:’ depending on the installation destination). This value does not end with ‘\’ at the end.
HOMEPATH
Windows NT series / XP and later
\Users\<user-name>, \Documents and Settings\<user-name>, etc.
The absolute path, excluding the drive name, of the ‘Home Directory’ for the currently logged-on user. User-specific settings and document files, among other things, are stored within this directory.
Please note that ‘My Documents’ may not necessarily be ‘%HOMEDRIVE%%HOMEPATH%\Documents’. The path to ‘My Documents’ does not appear in the environment variables.
NUMBER_OF_PROCESSORS, PROCESSOR_ARCHITECTURE, PROCESSOR_IDENTIFIER, PROCESSOR_LEVEL, PROCESSOR_REVISION
Windows NT series / XP and later
Values indicating the number and types of processors, among other things. Determined by the system.
COMPUTERNAME
Windows NT series / XP and later
Contains the computer name of the system.
USERNAME
Windows NT series / XP and later
SYSTEM, Administrator, etc.
Contains the name of the user currently running the program. If the system is running it directly, such as for a ‘service’, it will be ‘SYSTEM’.
USERDOMAIN
Windows NT series / XP and later
Contains the domain name of the user currently running the program. If the system is running it directly, such as for a ‘service’, it may not exist.

Notices

The timing of environment variable expansion is generally before the execution of the command. Therefore, depending on the contents of the environment variables, it may not be possible to execute the command correctly. For example,

if not %OS%==Windows_NT goto NotSupported

... this is executed as follows when the value of ‘OS’ does not exist.

if not ==Windows_NT goto NotSupported

This syntax is invalid in MS-DOS and Windows 95/98/Me.

To avoid invalidity,

if not "%OS%"=="Windows_NT" goto NotSupported

... this is expanded as follows:

if not ""=="Windows_NT" goto NotSupported

... becoming executable in MS-DOS and Windows 95/98/Me as well.

[Windows NT series] In Windows NT-based systems (Command Prompt), the use of Set increases the characters that can be set in environment variables, so it is important to pay even more attention to the values of environment variables. For example,

set MYVAR1=^<hoge^>

... executing this sets the value ‘<hoge>’ into ‘MYVAR1’, so

echo %MYVAR1%

... this command line will be expanded to:

echo <hoge>

... and it is interpreted as a command containing redirection ‘<’ and ‘>’.

Samples

Sample 1

%COMSPEC% /k myprog1.bat

Assuming that the program/command is stored in the environment variable ‘COMSPEC’, this executes the program/command with the argument ‘/k myprog1.bat’.

Note that COMSPEC usually specifies the path to Command.com (MS-DOS, Windows 95/98/Me) or Cmd.exe (Windows NT series). For information about the ‘/k’ option, please refer to the respective pages.

Sample 2 (Batch file)

set TEMP_FILE=%TEMP%\tempdata.txt
echo AppVersion: %HOGE_VERSION%>"%TEMP_FILE%"
echo Language: %HOGE_LANGUAGE%>>"%TEMP_FILE%"
piyo.exe "%TEMP_FILE%"
del "%TEMP_FILE%"

Assuming that the environment variable ‘TEMP’ contains a directory, this sets the path pointing to the file ‘tempdata.txt’ within that directory as ‘TEMP_FILE’. Then, using the Echo command, ‘>’, and ‘>>’, this writes data to that file. Afterward, this specifies the file's path (the content of ‘TEMP_FILE’) as an argument for ‘piyo.exe’, executes it, and finally deletes the file.

Sample 3

set INPUT_DIR=.\Files
set OUTPUT_DIR=.\Output
trans.exe

Sets the environment variables ‘INPUT_DIR’ and ‘OUTPUT_DIR’ and executes ‘trans.exe’. Since ‘trans.exe’ is executed within a new environment copied from the current environment, the values set for these environment variables here can be used within ‘trans.exe’. Therefore, by pre-setting the environment variables that the program uses, you can control the behavior of the program.

Sample 4

set /p MY_OPTION="Enter the option:"
foo.exe /option:"%MY_OPTION%"

[Windows NT series] [Extensions] Uses the Set command to prompt for arbitrary input for the value of the environment variable ‘MY_OPTION’ and passes that value as an argument to ‘foo.exe’. To prevent unintended behavior, the value of ‘MY_OPTION’ is enclosed in double quotes (" "). (Note: In this case, ‘foo.exe’ needs to correctly interpret the double quotes and their contents.)

Sample 5 (Batch file)

setlocal enabledelayedexpansion
nmake || exit /b !errorlevel!
bar

[Windows NT series] [Extensions] Enables the use of ‘!’ with Setlocal and then executes ‘nmake’ command. If ‘nmake’ command fails (returns a non-zero exit code), the batch file is terminated using the Exit command (see also "||"). In this code, when using exit codes,

nmake || exit /b %errorlevel%

... using ‘%errorlevel%’ like the above treats the commands connected by ‘||’ as a single ‘block’. Therefore, before executing ‘nmake’, all the environment variables included in this ‘block’ are expanded, and the code will be as following:

nmake || exit /b 0

... resulting it will not be able to return the correct exit code if ‘nmake’ fails.