Set command in Windows
The set command helps show, set or remove CMD.EXE environment variables. If used without parameters, the set command displays the current environment variable settings.To better understand how to use this command, see the example below.
Order set
- Set command syntax
- Command parameter set
- Command set statement
- Use environment variables
- Show current environment settings
- Use parameters
- Use the / a parameter
- Support to extend delayed environment variables
- Work with command extensions
- Call a set variable from a batch file
- Use the set command in Recovery Console
- Example set command
Set command syntax
set [= []]
set [/ p] = []
set / a =
Command parameter set
Parameters
Describe
Specify an environment variable to proceed with the installation or modification.
Specifies the string to bind to the specified environment variable.
/ p
Set the value of the variable for an input stream entered by the user.
Optional parameters.Specifies a message to prompt the user to enter data.This parameter is used along with the / p command line option.
/ a
Set the string to numerical expression according to estimates.
Specify a numeric expression.See annotations for valid operators that can be used in expressions for more details.
/?
Show help at thecommand prompt.
Command set statement
Use the set command with the command extension enabled
When the command extension is enabled (default) and you run the set command with a specific value, it displays all variables that begin with that value.
Use special characters
The characters <,>, |, &, ^ are special command characters (command shells) and they must be preceded by escape characters (^) or enclosed in quotation marks when used in strings (eg "StringContained & Symbol").If you use quotation marks to attach a string containing one of the special characters, quotation marks will be set as part of the environment variable value.
Use environment variables
Use environment variables to control the behavior of some files and sequence programs and to control how Windows and MS-DOS subsystems appear and work.The set command is often used in Autoexec.nt file to set environment variables.
Show current environment settings
When you enter only one set command, the current environment settings are displayed.These settings usually include the COMSPEC and PATH environment variables, used to help find programs on the drive.Two other environment variables are also used by Windows as PROMPT and DIRCMD.
Use parameters
When you specify values for variables and strings, the value of the specified variable will be added to the environment and the string associated with that variable.If the variable already exists in the environment, the new string value will replace the old string value.
If you specify a variable and an equal sign (do not specify a string) for the set command, the string value associated with the variable will be deleted (as if the variable is not there).
Use the / a parameter
The following table lists the supported operators for the / a parameter in descending order of priority:
Operator
Operation is performed
()
Grouping
!~ -
Unilateral
* /%
Numerals
+ -
Numerals
<< >>
Move logic
&
Island bit AND
^
Reverse OR bit
= * = / =% = + = - = & = ^ =
= << = >> =
,
Expression separator
If you use the logical operator (&& or ||) or the module (%), place the expression string in quotation marks.Any string other than numbers in the expression is treated as an environment variable name and their values will be converted into numbers before they can be processed.If you specify an environment variable name that is not defined in the current environment, the value 0 is allocated, allowing you to perform arithmetic with the environment variable values without using the% operator to get value.
If you run the set / a command from the command line outside the script, it will display the final value of the expression.
The numeric value will be a decimal number unless there is a prefix equal to 0 × for the hexadecimal or 0 for the octal number.Therefore, 0 × 12 is the same as 18, equivalent to 022.
Support to extend delayed environment variables
By default, support for extended delay environment variables will be disabled, but you can turn it on or off using cmd / v .
Work with command extensions
When the command extension is enabled (default) and you run the set command alone, it displays all the current environment variables. If you run the set command with a specific value, it will display variables that match that value.
Use the set command in batch files
When creating batch files, you can use the set command to create variables and then use them in the same way you would with variables that are numbered 0 0 to% 9.You can also use% 0 to% 9 variables as input to the set command.
Call a set variable from a batch file
When you call a variable value from a batch file, set that value in percent sign (%).For example, if your batch program creates an environment variable called BAUD, you can use the string associated with BAUD as an alternative parameter by entering% baud% at the command prompt command prompt.
Use the set command in Recovery Console
The set command, with different parameters, will be available in the Recovery Console.
Example set command
To set an environment variable named TEST ^ 1, enter:
set testVar = test ^^ 1
Note
The set command assigns everything after the equals sign (=) to the value of the variable.If you type:
set testVar = "test ^ 1"
You will get the following result:
testVar = "test ^ 1"
To set an environment variable named TEST & 1, enter:
set testVar = test ^ & 1
To set an environment variable named INCLUDE so that it can be linked to the string C: Inc (folder Inc on drive C), type:
set include = c: inc
You can then use the C: Inc string in batch files by placing INCLUDE in percent sign (%).For example, you can include the following command in a batch file to display the contents of the directory associated with the INCLUDE environment variable:
dir% include%
When this command is processed, the string C: Inc will replace% include%.
You can also use the set command in a batch program, when a new directory will be added to the PATH environment variable.For example:
@echo off
rem ADDPATH.BAT adds a new directory
rem vào biến môi trường đường dẫn.
đặt đường dẫn =% 1;% path%
set
To display a list of all environment variables starting with P, type:
set p
Note:
This command will require command extensions to be enabled by default.
See more:
- How to create disk full BAT file repair Windows 10
- Overview of Windows Server 2008
- Basic Linux commands everyone needs to know
- Summary of 20 common Run commands on Windows operating system
You should read it
- Del command in Windows
- Rem command in Windows
- Cmd command in Windows
- Command at in Windows
- Fc command in Windows
- The echo command in Windows
- Bitsadmin getdisplayname and bitsadmin geterror command in Windows
- The command reg add in Windows
- Setx command in Windows
- Reg command copy in Windows
- Doskey command in Windows
- Exit command in Windows