Purpose: | Display, create, modify, or delete environment variables |
Format: | Display mode: |
SET [/= /D /E /P /S /U /V /X] [wildname]
Definition mode:
SET [/= /A /B /D /M /O /S /U /V /E /RO /R [file...] /T:type | name=value | prompt ]
Deletion mode:
SET [/= /D /M /S /U /V /E ] name=
file | One or more input files from which to read variable definitions. |
name | The name of the environment variable. |
value | The new value for the variable, separated from name by space[s]. |
prompt | Optional input prompt for the /P name= option. |
wildname | Name of variable[s] to be displayed. May contain * wildcard unless displaying registry variables. |
Arithmetic |
Read from file(s) |
||
Batch variable |
Readonly variable |
||
Default |
System |
||
Environment, too |
/T:type |
Set variable type |
|
Master environment |
User |
||
Pause or Prompt |
Volatile |
||
Don't overwrite |
Override VariableExclude |
||
Don't echo /A result |
File Completion Syntax:
The default filename completion syntax is: [/r] * [1] variables [2*] *
Usage:
Every program and command inherits an environment, which is a list of pairs of variable names and values. Each value is a non-empty character string (i.e., there must be at least one character in it). Many programs use entries in the environment to modify their own actions. TCC itself uses several environment variables.
If you simply type the SET command with no options or parameters, it will display all the names and values of all currently defined variables in the environment. Typically, you will see an entry called PATH, an entry called CMDLINE, and whatever other environment variables you and your programs have established:
[c:\] set
PATH=C:\;C:\UTIL
CMDLINE=C:\TCMD\TCSTART.CMD
If you enter only name, and there is no variable with that name, SET will display all environment variables whose names begin with name. For example, if there is no variable pa, the command below will display all variables whose names start with pa:
set pa
The above command is equivalent to the command
set pa*
If there is only a single parameter and it contains one or more wildcards (sorry, only * available), SET will display all matching environment variables. You cannot use wildcards to display the registry variables (/D, /S, /U, and /V).
You can specify variables to exclude from the SET display with the VariableExclude variable. For example, to suppress the display of the processor and user variables:
set VariableExclude=proc*;user*
(Note that this option doesn't affect the existence of the variables, just whether they're displayed by a SET with no arguments.)
To add a variable to the environment, type SET, a space, the variable name, an equal sign, and the desired value:
set mine=c:\finance\myfiles
The variable name and the text after the equal sign will be left just as you entered it. However, case is ignored when looking for a variable; for example MyVar, myvar, and MYVAR all refer to the same variable. If the variable already exists, its value will be replaced with the new text that you entered.
Normally you should not put a space on either side of the equal sign. A space before the equal sign will become part of the name ; a space after the equal sign will become part of the value.
Trailing whitespace in the SET command is ignored. To create a variable with trailing whitespace, use a pair of back quotes after the whitespace:
set mine=%@repeat[ ,20]``
makes mine 20 characters of spaces.
If you use SET to create a variable with the same name as one of the TCC internal variables, you will disable the internal variable. If you later execute a batch file or alias that depends on that internal variable, it may not operate correctly. Once you delete your variable, the internal variable becomes accessible again.
To display the contents of a variable, type SET plus the variable name:
set mine
You can edit environment variables with the ESET command. To remove variables from the environment, use UNSET, or type SET, followed by the variable name and an equal sign:
set mine=
The variable's name is limited to a maximum of 1024 characters.
Note: You cannot use SET to modify GOSUB variables.
The size of the environment is set automatically, and increased as necessary as you add variables.
Registry Variables
Windows stores some of its own variables in the registry. This includes Default, System, User, and Volatile variables. Those variables can be manipulated with the SET command's /D, /S, /U and /V options respectively. For example, to display the contents of volatile variable clientname, use:
set /v clientname
Note that setting a registry variable using one of the options /D, /S, /U or /V will not set the variable in the local environment unless you also use the /E option.
User variables are user-specific, and volatile variables are only valid for the current Windows session. Use caution when directly modifying registry variables as they may be essential to various Windows processes and applications.
If the Update Environment on System Change configuration option is set, TCC will monitor the WM_SETTINGCHANGE message and update the environment from the User, Volatile, and System registry entries. The update is done whenever TCC displays the prompt (to prevent the environment from changing in the middle of a command).
Array Variables
In addition to environment variables, SET is also used to set values for array variables. For example, to define a 5-row by 10-column array, you would first use SETARRAY:
setarray array1[5,10]
To set the array values (0-based), the syntax is:
set array1[a[,b[,c[,d]]]
For example:
set array1[0,0]=Bob
set array1[0,1]=Bob's Job
To expand the array variable:
echo Name is %array[0,0] and job is %array1[0,1]
Options:
/= | Display the SET command dialog to help you set the command line options. The /= option can be anywhere on the line; additional options will set the appropriate fields in the command dialog. |
set /a var=2+2
/A interprets non numeric strings in value as environment variable names whether or not preceded by a percent sign %, and replaces them with their respective values. For example, this sequence will set Y to 4: |
set x=2
set /a y=x+2
You can use @EVAL to perform the same task; SET /A is included for compatibility with CMD. Unlike @EVAL, use of the >> or << shift operators in SET /A requires disabling their interpretation as redirection symbols by using SETDOS /X-6. |
/B | Set a batch variable (%1 - %n). Only valid when TCC is executing a batch file. The batch variable %n must already exist. |
/E | When used together with one of /D, /S, /U, or /V, set both the registry variable and the local environment variable. |
/M | If a variable name is specified, change it to the original value when TCC started. If no name is specified, revert the entire environment to the original environment when TCC started. |
/P | When used without a variable name, wait for a key to be pressed after each screen page before continuing the display. Your options at the prompt are explained in detail under Page and File Prompts. |
When used with a variable name and an optional prompt string, e.g. set /p myvar=Enter value, emulates the CMD behavior of allowing entry of a value for the variable. This is provided for compatibility reasons only. For more flexibility, use the TCC ESET or INPUT commands.
/R | Read environment variables from a file. This is much faster than loading variables from a batch file with multiple SET commands. Each entry in the file must fit within the command line length limit for TCC. The file is in the same format as the SET display (i.e., name=value), so SET /R can accept as input a file generated by redirecting SET output. For example, the following commands will save the environment variables to a file, and then reload them from that file: |
set > varlist
set /r varlist
You can load variables from multiple files by listing the filenames individually after the /R.
If you are creating a SET /R file by hand, and need to create an entry that spans multiple lines in the file, you can do so by terminating each line (except the last) with an escape character. However, you cannot use this method to exceed the command line length limit. You can also add comment lines to the file by starting each with a colon :. You can also use other special characters, e.g., trailing whitespace, redirection and pipe symbols (<>|), without the need for escaping the characters. If you reference the value of another variable in value (e.g., x=%path;c:\jpsoft), evaluating that variable (path in the example) is postponed until at some future time a command line evaluates the current variable (x in the example), so that the command echo %x will display the path in effect when echo is executed, regardless of what path may have been when the original SET defined x.
If you do not specify a filename and input is redirected, SET /R will read from stdin.
/RO | Create a read-only variable. Once you have set the variable, you cannot change it or UNSET it. Only environment variables can be read-only, not registry variables or array variables. A read-only variable will automatically be exported from an ENDLOCAL. |
/S | Create/modify/delete a system variable in the registry (HKLM\System\CurrentControlSet\Control\Session Manager\Environment). |
/T:type[:"regex"] | Set a variable type. If you try to set the variable to an incompatible type, SET will return an error. The supported types are: |
int (or 1) | The variable can only contain 0-9 |
dec (or 2) | The variable can only contain 0-9, the decimal character, and the thousands separator |
hex (or 3) | The variable can only contain 0-9 and A-F |
bool (or 4) | The variable can only contain 0 or 1 |
alpha (or 5) | The variable can only contain A-Z and a-z |
alnum (or 6) | The variable can only contain A-Z, a-z, and 0-9 |
regex (or 7) | The variable must match the specified regular expression |