Set - DOS/Command Prompt Reference
Displays the environment variable list for the current environment (prompt), or adds/modifies/deletes a variable.
set /A <var-name>[=<expression>]
set /P <var-name>=[<prompt>]
|(no parameters)||Lists the environment variables for the current environment.|
|<var-name>||Specifies the name of the environment variable.|
Specifies the value to store in the environment variable. If omitting (writing <var-name> and ‘=’), the variable <var-name> will be removed from the current environment.
[Windows NT series] [Extensions] Another environment variable(s) can be used for <value> by using ‘%’.
|/A <var-name>[=<expression>]||[Windows NT series] [Extensions] Treats <expression> as an arithmetic expression, and stores its result in the variable <var-name>. If executing on the Command Prompt, prints its value (result) to the screen. Numerics in the expression are used for calculation, and non-numeric characters are treated as the environment variable name, expanded during calculation. For available symbols (operators), please see Details section. If ‘=’ and following characters are omitted, does nothing for the variable (only prints its value on the Command Prompt).|
|/P <var-name>=[<prompt>]||[Windows NT series] [Extensions] Prompts the user for a value and stores it in the variable <var-name>. By specifying <prompt> it will be displayed as a prompt (, and the prompt cursor will be placed just after <prompt> string). ‘=’ cannot be omitted, but <prompt> can be omitted.|
Set command is used to treat environment variables. Basically ‘set name=value’ form is used.
About environment variables
‘Environment variables’ are ones of the configurations of the current prompt (Command.com or Cmd.exe) and programs run at the prompt use these values. The Set command allows you to add new environment variables, edit or delete variables in the current prompt (environment).
* Environment variables in system settings cannot be changed with Set. To change system settings, edit Autoexec.bat for MS-DOS/Windows 95/98/Me, or open ‘System Property’ for Windows NT series. ([Windows Vista or later] Setx is available for changing environment variables in system settings.)
To use environment variables in batch programs or etc., surround the variable name with ‘%’ characters, resulting expanding to its value automatically.
[MS-DOS, Windows 95/98/Me] The system might display ‘Out of environment space’ when the available environment space (allocated to the current prompt) is insufficient. To change (increase) this space, use ‘Command’ command ‘/E’ option.
Common environment variables are ‘DIRCMD’ (holding default options for Dir command), ‘COPYCMD’ (holding default options for Copy, Move, and Xcopy), ‘COMSPEC’, ‘PROMPT’, and ‘PATH’.
For about environment variables, please see ‘Using environment variables’.
Extended expansion of environment variables
[Windows NT series] [Extensions] When using environment variables, you can use special syntax to change behavior of expansion of environment variables.
For details, see ‘%’ page.
Operators available in /A switch
[Windows NT series] [Extensions] The operators used with /A option are follows. Note that numerics are treated as signed 32-bit integers. In the following table, ‘A’ and ‘B’ refer to expressions (numeric or an expression surrounded with parentheses).
|8||(A)||Putting an expression inside parentheses takes precedence.|
|7||!A||[Not] 1 if A is 0, or 0 otherwise.|
|~A||[Bit reversal] Adds 1 to A and inverts its sign.|
|6||A * B||[Multiplication]|
|A / B||[Division]|
|A % B||[Remainder] Represents the remainder when A is divided by B.|
|5||A + B||[Addition]|
|A - B||[Subtraction]|
|4||A << B||[Logical left shift] Multiplies A by ‘2 to the power of B’.|
|A >> B||[Logical right shift] Divides A by ‘2 to the power of B’.|
|3||A & B||[Logical AND]|
|2||A ^ B||[Exclusive OR]|
|1||A | B||[OR]|
|0||<var-name2> = B||[Assignment] Assigns the result of B to <var-name2>, and uses its result. In addition to ‘=’, ‘+=’, ‘-=’, ‘*=’, ‘/=’, ‘%=’, ‘&=’, ‘|=’, ‘^=’, ‘<<=’, and ‘>>=’ are also available.
This expression will add or replace the variable <var-name2> as a (new) environment variables.
|-1||A, B||[Delimiter] Calculates A and B in order, and makes B's result as an entire result. Only B's result is used but A is also calculated; if A contains an assignment expression, a new environment variable is added.|
Removes the environment variable ‘MYVAR’ from the current environment if exists.
Adds ‘D:\bin;’ to the head of ‘PATH’ environment variable.
set /P TEMPVAR=Hello< NUL
[Windows NT series] [Extensions] Prints ‘Hello’ to the screen without newline. ‘set /P’ outputs prompt for input without newline and waits for input, so giving ‘NUL’ with input redirection results to output texts only.
* In this sample ‘TEMPVAR’ environment variable will be removed if exists. To avoid removing environment variables unexpectedly, consider using Setlocal command.
Sample 4 [Batch file]
@echo off setlocal if not "%SETTING_PATH%"=="" goto check_path :input_path set SETTING_PATH= echo Input the path containing 'settings.xml'. (enter with blank to exit) set /P SETTING_PATH="> " if "%SETTING_PATH%"=="" goto end :check_path if not exist "%SETTING_PATH%\settings.xml" ( echo 'settings.xml' is not found in path '%SETTING_PATH%'. goto input_path ) TheProgram.exe /E :end
[Windows NT series] [Extensions] Checks environment variable ‘SETTING_PATH’ and, if not set, prompts for input by using ‘set /P’. Since the message for prompt is long, the message is printed by Echo, and only ‘< ’ is printed in the prompt line.
Note that entering Ctrl+C during waiting for input breaks the process.
* Since the prompt string has ‘>’ character, enclosing with ‘" "’ is necessary not to be considered as the output redirection.
Sample 5 [Batch file]
@echo off setlocal call :strlen "Test String" echo %STRLEN_RET% goto end :strlen set STRLEN_RET=0 set STRLEN_TEMP=%~1 :strlen_loop if "%STRLEN_TEMP%"=="" exit /B 0 set /A STRLEN_RET="STRLEN_RET+1" set STRLEN_TEMP=%STRLEN_TEMP:~1% goto strlen_loop :end
[Windows NT series] [Extensions] This is a batch file sample for ‘strlen’, which returns the length of a string passed as a parameter. This sets the result to ‘STRLEN_RET’ environment variable. Line 12 increments the numeric value, and line 13 cuts the first character of the value of ‘STRLEN_TEMP’ variable. Note that line 9 uses extension syntax to remove double quotation marks from a parameter passed to the batch file. (Please see also the description of ‘%’.)