Start-> Run, enter: Set press ENTER
You can list the current system environment variables and user environment variables. 1:
(Figure 1)
You can set a user environment variable. Enter: Set hosts = C:/Windows/system32/Drivers/etc and press Enter.
We can use the set press enter to list the current system environment variables and user environment variables. We can find that the hosts environment variables are also in the list,
This variable is successfully set. In cmd, enter cd % hosts % and press Enter.
You can enter the C:/Windows/system32/Drivers/etc directory. 2:
(Figure 2)
Note: The SET command can only set environment variables that are valid under the current cmd. If we close the current command line, open another
Enter cd % hosts % in the command line prompt and press Enter. The system will not find the path and report an error. If we are in the current command line
Enter: Start and press enter to open a new command line. The hosts variable will not expire.
There is a way to keep the hosts environment variable effective without restarting the computer!
Create a batch file: var. cmd
Enter the following content:
@ Echo off
Tskill Explorer
Set hosts = C:/Windows/system32/Drivers/etc
Start Explorer
Pause
Save and double-click it.
The following is a detailed description of the SET command:
Set [variable = [String]
Variable specifies the environment variable name.
String specifies a series of strings to be assigned to a variable.
To display the current environment variable, type A set without parameters.
If the command extension is enabled, set changes as follows:
You can use only one variable to activate the SET command. The equal sign or value does not show any prefix matching.
The value of all variables of the name used by the set command. For example:
Set P
All the variables starting with the letter P are displayed.
If the variable name cannot be found in the current environment, the set command sets errorlevel
Set to 1.
The SET command does not allow variable names to contain equal signs.
Two new command line switches are added to the set command:
Set/A expression
Set/P variable = [promptstring]
/A command line switch specifies the string on the right of the equal sign as the evaluated numeric expression. This expression
The evaluator is simple and supports the following operations in descending priority order:
()-Group
! ~ --Unary operator
*/%-Arithmetic Operator
+--Arithmetic Operator
<>-Logical shift
-Bitwise "and"
^-Bitwise "exception"
|-Bitwise "or"
= * =/= % = + =--Value assignment
<=>>=
,-Expression Separator
If you use any logical or remainder operators, you must use the expression string
Enclose the quotation marks. Any non-numeric string key in the expression as an environment variable
The environment variable name value has been converted to a number before use. If you specify
Environment variable name, but not defined in the current environment, the value will be set
Zero. This allows you to use the environment variable value for calculation without typing those % symbols.
To obtain their values. If set/A is executed by the command line outside the command script,
Then it displays the final value of the expression. The allocation operator is in the allocation Operator
An environment variable name is required on the left. In addition to the hexadecimal format, the prefix 0x and octal
A decimal number with a prefix of 0. Therefore, 0x12 and 18 and 022
Same. Note that the octal formula may be confusing: 08 and 09 are invalid numbers,
Because 8 and 9 are not valid octal digits.
The/p command line switch allows you to set the variable value to one line of user input. Read Input
The specified promptstring is displayed before the row. Promptstring can be empty.
Environment variable replacement has been enhanced as follows:
% PATH: str1 = str2 %
The path environment variable is extended, and "str2" is used to replace each "str1" in the extended result ".
To effectively delete all "str1" and "str2" from the extension results, it can be empty.
"Str1" can start with a star. In this case, "str1" will expand the result from
From the beginning to the place where the rest of str1 appears for the first time, they always match.
The extension can also be a substring.
% PATH :~ 10, 5%
The path environment variables will be extended, and then only 11th (partial
10 characters. If no length is specified, the default value is used.
Value, that is, the remainder of the variable value. If both numbers (offset and length) are negative,
The number used is the length of the environmental variable value plus the specified offset or length.
% PATH :~ -10%
Extracts the last 10 characters of the PATH variable.
% PATH :~ 0,-2%.
All characters in the PATH variable, except the last two.
Added support for extended delayed environment variables. This support is always set by default
Disabled, but it can also be enabled/disabled through the/V command line switch of CMD. EXE.
See CMD /?
The latency environment takes into account the current extended restrictions encountered when reading a line of text
Variable Expansion is useful, rather than execution. The following example shows how to directly
Variable Expansion problems:
Set Var = before
If "% var %" = "before "(
Set Var = after
If "% var %" = "after" @ echo if you see this, it worked
)
The message is not displayed, because when the first if statement is read, The both if statement
% Var % will be replaced; the reason is: it contains the if style, if is
Compound statement. Therefore, the IF statement in the composite statement is actually comparing the "before" and
"After", the two will never be equal. Similarly, the following example will not
Expected results:
Set list =
For % I in (*) do set list = % List % I
Echo % List %
The reason is that it does not create a file list in the current directory, but just
The list variable is set to the last file found. This is also because % List %
When the for statement is read, it is extended only once. Moreover, the List variable at that time
Is empty. Therefore, the for loop we actually execute is:
For % I in (*) do set list = % I
This loop continues to set the list to the last file found.
Latency Environment Variable Expansion allows you to use a different character (Exclamation point) during execution
Time expansion environment variable. If the extended variable is enabled
The example is written as follows to achieve the expected results:
Set Var = before
If "% var %" = "before "(
Set Var = after
If "! VaR! "=" After "@ echo if you see this, it worked
)
Set list =
For % I in (*) do set list =! List! % I
Echo % List %
If the command extension is enabled, several dynamic environment variables can be extended,
Does not appear in the list of variables displayed in set. Each time the variable value is extended,
These variable values are dynamically calculated. If the user uses any of these names
A definition variable replaces the dynamic definition described below:
% Cd %-extended to the current directory string.
% Date %-extend to the current date in the same format as the date command.
% Time %-extend to the current time in the same format as the time command.
% Random %-any decimal number between 0 and 32767.
% Errorlevel %-extend to the current errorlevel value.
% Cmdextversion %-extended to the current command processor extension version number.
% Worker line %-extended to the original command line that calls the command processor.