Although using `set /?` can also get the usage of the `set` command, the translation is really not commendable, and I don't understand English very well. After several twists and turns, I found a very good article on the Internet introducing the `set` command. The author added his own insights while citing the `set /?` help file and made easy-to-understand explanations, which is very suitable for rookies like me.
If there is already the same or similar post in the forum, please ask the moderator to delete this post.
Detailed Explanation of the `set` Command. Very Helpful for Newcomers
I haven't posted for a long time. Today I'm going to write a beginner's teaching post about BAT!
In the previous post, I briefly introduced the role of `SET` in setting custom variables. Now I will specifically talk about other functions of `set`.
First, review its usage for setting custom variables.
Example:
@echo off
set var=I am the value
echo %var%
pause
Please see `set var=I am the value`, this is the method for BAT to directly set variables in the batch processing! `set` is the command, `var` is the variable name, and the "I am the value" on the right of the equal sign is the value of the variable. In the batch processing, if we want to reference this variable, we enclose the variable name `var` with two `%` (percent signs), such as `%var%`.
This `SET` syntax can only directly assign the value of the variable in the advance of the BAT code. Sometimes we need to provide an interactive interface, let the user input the value of the variable by themselves, and then we will do corresponding operations according to this value. Now I will talk about this `SET` syntax, which only needs to add a "/P" parameter!
Example:
@echo off
set /p var=Please enter the value of the variable:
if %var% == 1 echo You entered 1 ~_~
pause
`set /p` is the command syntax, `var` is the variable name, and the "Please enter the value of the variable: " on the right of the equal sign is the prompt, not the value of the variable.
After running, we directly enter 1 after the prompt, and a line "You entered 1 ~_~" will be displayed. If you enter something else, there will be no response.
Okay, let's review here first, and now talk about other functions of `SET`.
Using `set /?` to view the help of `SET`, we find that in addition to the ones I mentioned above, `SET` also has the following several syntaxes:
SET ]
SET /P variable=
There are also the following syntaxes:
SET /A expression
Environment variable substitution has the following enhancements:
%PATH:str1=str2%
%PATH:~10,5%
%PATH:~-10%
%PATH:~0,-2%
What are the uses of these syntaxes? Now let's explain them one by one!
SET /A expression
The /A command switch specifies that the string on the right of the equal sign is the numerical expression to be evaluated. The expression evaluator is simple and supports the following operations in decreasing order of priority:
() - Grouping
! ~ - - Unary operators
* / % - Arithmetic operators
+ - - Arithmetic operators
<< >> - Logical shift
& - Bitwise "AND"
^ - Bitwise "XOR"
| - Bitwise "OR"
= *= /= %= += -= - Assignment
&= ^= |= <<= >>=
, - Expression separator
The above is the content in the system help. Does it look a bit confusing? It doesn't matter, let me explain it simply:
The /A parameter of `set` allows `SET` to support mathematical symbols for addition, subtraction and other mathematical operations!
Now start to give examples to introduce the usage of these mathematical symbols:
Look at the example. Please copy the command directly in CMD and run it, no need to save as BAT!
set /a var=1 + 1
The syntax of `set /a`, the variable name `var`, 1 + 1 is the mathematical expression.
After copying and running, it will directly display a 2, or after running, we enter `echo %var%`, it is also two, this is a simple addition operation!
set /a var=2 - 1 What is the result? If you can't see the result, just `echo %var%`.....
set /a var=2 * 2 Multiplication operation
set /a var=2 / 2 Division operation
set /a var=(1+1) + (1+1) The result is equal to 4. Can you understand it!
set /a a=1+1,b=2+1,c=3+1 After running, a 4 will be displayed, but when we use `echo %a% %b% %c%` to see the result, we will find that other mathematical operations also have effects! This is the role of the "comma"!
Sometimes we need to directly perform addition and subtraction operations on the original variable, and we can use this syntax.
set /a var+=1 This syntax corresponds to the original syntax `set /a var = %var% + 1`, which has the same result, and performs mathematical operations on the value of the original variable again, but this way is simpler.
Another one:
set /a var*=2 The others are used like this, as long as there is this syntax in the help.
In addition, there are some logical OR and remainder operators. Using these symbols according to the above method will report an error.
For example, if we enter `set /a var=1 & 1` (AND operation) in CMD, it will not display 1, but report an error. Why? For such "logical OR and remainder operators", we need to enclose them in double quotes. Look at the example.
set /a var= 1 "&" 1 In this way, the result will be displayed. Other logical OR and remainder operator usages:
set /a var= 1 "+" 1 XOR operation
set /a var= 1 "%" 1 Modulo operation
set /a var= 2 "<<" 2 Exponentiation operation
set /a var= 4 ">>" 2 I don't remember the name in mathematics very well....
There are a few more that are not very good at mathematics and can't be clarified.... I won't list them.
These symbols can also be used in simple usages like &= ^= |= <<= >>=. For example:
set /a var"&=" 1 is equal to `set /a var = %var% "&" 1` Note the quotes.
Okay, the symbols are talked about here. Now talk about %PATH:str1=str2%.
This is to replace the content of the variable value. Look at the example.
@echo off
set a= bbs.verybat.cn
echo The value before replacement: "%a%"
set var=%a: =%
echo The value after replacement: "%var%"
pause
By comparing, we find that it replaces the space in the variable %a%. From this example, we can find that the operation of %PATH:str1=str2% is to replace all str1 in the variable %PATH% with str2.
For example, let's change the above example to this:
@echo off
set a=bbs.verybat.cn
echo The value before replacement: "%a%"
set var=%a:.=Brainstorm%
echo The value after replacement: "%var%"
pause
Explanation: `set var=%a:.=Brainstorm%`
The `set` command, the variable name `var`, the word `a` is the value of the variable to be character-replaced, "." is the value to be replaced, and "Brainstorm" is the value after replacement!
After execution, all "." in the variable %a% will be replaced with "Brainstorm".
This is a very good function of `set` to replace characters! Let's talk about this first.
%PATH:~10,5% What does this mean? Look at the example:
@echo off
set a=bbs.verybat.cn
set var=%a:~1,2%
echo %var%
pause
After execution, we will find that only the two letters "bs" are displayed. The value of our variable %a% is not bbs.verybat.cn? How come only the second and third letters "bs" are displayed? After analyzing the result, we can easily see that %PATH:~10,5% is to display the value of the specified number of bits in the variable PATH!
Analyze `set var=%a:~1,2%`
The `set` command, the variable value `var`, `a` is the variable to be character-operated, "1" starts from the fewth bit of the variable "a", "2" displays several bits, and together it is to assign the value of the variable a from the first bit, and assign the last two bits to the variable var.
Well, it should be understood.
The other two syntaxes:
%PATH:~-10%
%PATH:~0,-2%
They also mean to display the value of the specified number of bits of the specified variable.
%PATH:~-10% Example:
@echo off
set a=bbs.verybat.cn
set var=%a:~-3%
echo %var%
pause
This is to assign the value of the last 3 bits of the variable a to the variable VAR.
Of course, we can also change it to this:
@echo off
set a=bbs.verybat.cn
set var=%a:~3%
echo %var%
pause
This is to assign the value of the first 3 bits of the variable a to the variable VAR.
%PATH:~0,-2% Example:
@echo off
set a=bbs.verybat.cn
set var=%a:~0,-3%
echo %var%
pause
After execution, we find that "bbs.verybat" is displayed, missing ".cn".
From the result analysis, it is easy to analyze that this is to display the value of the variable a from bit 0, and display the value of the bit of the total number of bits of the variable a minus 3 (the value of our variable a is bbs.verybat has 11 bits, 11-3=8), so it only displays the value from bit 0 to bit 8, and assigns it to the variable var.
If you change it to this:
@echo off
set a=bbs.verybat.cn
set var=%a:~2,-3%
echo %var%
pause
Then it will display the value from bit 2 to bit 8, and assign it to the variable var.
[ Last edited by 1112yuhua on 2007-9-12 at 01:29 PM ]
If there is already the same or similar post in the forum, please ask the moderator to delete this post.
Detailed Explanation of the `set` Command. Very Helpful for Newcomers
I haven't posted for a long time. Today I'm going to write a beginner's teaching post about BAT!
In the previous post, I briefly introduced the role of `SET` in setting custom variables. Now I will specifically talk about other functions of `set`.
First, review its usage for setting custom variables.
Example:
@echo off
set var=I am the value
echo %var%
pause
Please see `set var=I am the value`, this is the method for BAT to directly set variables in the batch processing! `set` is the command, `var` is the variable name, and the "I am the value" on the right of the equal sign is the value of the variable. In the batch processing, if we want to reference this variable, we enclose the variable name `var` with two `%` (percent signs), such as `%var%`.
This `SET` syntax can only directly assign the value of the variable in the advance of the BAT code. Sometimes we need to provide an interactive interface, let the user input the value of the variable by themselves, and then we will do corresponding operations according to this value. Now I will talk about this `SET` syntax, which only needs to add a "/P" parameter!
Example:
@echo off
set /p var=Please enter the value of the variable:
if %var% == 1 echo You entered 1 ~_~
pause
`set /p` is the command syntax, `var` is the variable name, and the "Please enter the value of the variable: " on the right of the equal sign is the prompt, not the value of the variable.
After running, we directly enter 1 after the prompt, and a line "You entered 1 ~_~" will be displayed. If you enter something else, there will be no response.
Okay, let's review here first, and now talk about other functions of `SET`.
Using `set /?` to view the help of `SET`, we find that in addition to the ones I mentioned above, `SET` also has the following several syntaxes:
SET ]
SET /P variable=
There are also the following syntaxes:
SET /A expression
Environment variable substitution has the following enhancements:
%PATH:str1=str2%
%PATH:~10,5%
%PATH:~-10%
%PATH:~0,-2%
What are the uses of these syntaxes? Now let's explain them one by one!
SET /A expression
The /A command switch specifies that the string on the right of the equal sign is the numerical expression to be evaluated. The expression evaluator is simple and supports the following operations in decreasing order of priority:
() - Grouping
! ~ - - Unary operators
* / % - Arithmetic operators
+ - - Arithmetic operators
<< >> - Logical shift
& - Bitwise "AND"
^ - Bitwise "XOR"
| - Bitwise "OR"
= *= /= %= += -= - Assignment
&= ^= |= <<= >>=
, - Expression separator
The above is the content in the system help. Does it look a bit confusing? It doesn't matter, let me explain it simply:
The /A parameter of `set` allows `SET` to support mathematical symbols for addition, subtraction and other mathematical operations!
Now start to give examples to introduce the usage of these mathematical symbols:
Look at the example. Please copy the command directly in CMD and run it, no need to save as BAT!
set /a var=1 + 1
The syntax of `set /a`, the variable name `var`, 1 + 1 is the mathematical expression.
After copying and running, it will directly display a 2, or after running, we enter `echo %var%`, it is also two, this is a simple addition operation!
set /a var=2 - 1 What is the result? If you can't see the result, just `echo %var%`.....
set /a var=2 * 2 Multiplication operation
set /a var=2 / 2 Division operation
set /a var=(1+1) + (1+1) The result is equal to 4. Can you understand it!
set /a a=1+1,b=2+1,c=3+1 After running, a 4 will be displayed, but when we use `echo %a% %b% %c%` to see the result, we will find that other mathematical operations also have effects! This is the role of the "comma"!
Sometimes we need to directly perform addition and subtraction operations on the original variable, and we can use this syntax.
set /a var+=1 This syntax corresponds to the original syntax `set /a var = %var% + 1`, which has the same result, and performs mathematical operations on the value of the original variable again, but this way is simpler.
Another one:
set /a var*=2 The others are used like this, as long as there is this syntax in the help.
In addition, there are some logical OR and remainder operators. Using these symbols according to the above method will report an error.
For example, if we enter `set /a var=1 & 1` (AND operation) in CMD, it will not display 1, but report an error. Why? For such "logical OR and remainder operators", we need to enclose them in double quotes. Look at the example.
set /a var= 1 "&" 1 In this way, the result will be displayed. Other logical OR and remainder operator usages:
set /a var= 1 "+" 1 XOR operation
set /a var= 1 "%" 1 Modulo operation
set /a var= 2 "<<" 2 Exponentiation operation
set /a var= 4 ">>" 2 I don't remember the name in mathematics very well....
There are a few more that are not very good at mathematics and can't be clarified.... I won't list them.
These symbols can also be used in simple usages like &= ^= |= <<= >>=. For example:
set /a var"&=" 1 is equal to `set /a var = %var% "&" 1` Note the quotes.
Okay, the symbols are talked about here. Now talk about %PATH:str1=str2%.
This is to replace the content of the variable value. Look at the example.
@echo off
set a= bbs.verybat.cn
echo The value before replacement: "%a%"
set var=%a: =%
echo The value after replacement: "%var%"
pause
By comparing, we find that it replaces the space in the variable %a%. From this example, we can find that the operation of %PATH:str1=str2% is to replace all str1 in the variable %PATH% with str2.
For example, let's change the above example to this:
@echo off
set a=bbs.verybat.cn
echo The value before replacement: "%a%"
set var=%a:.=Brainstorm%
echo The value after replacement: "%var%"
pause
Explanation: `set var=%a:.=Brainstorm%`
The `set` command, the variable name `var`, the word `a` is the value of the variable to be character-replaced, "." is the value to be replaced, and "Brainstorm" is the value after replacement!
After execution, all "." in the variable %a% will be replaced with "Brainstorm".
This is a very good function of `set` to replace characters! Let's talk about this first.
%PATH:~10,5% What does this mean? Look at the example:
@echo off
set a=bbs.verybat.cn
set var=%a:~1,2%
echo %var%
pause
After execution, we will find that only the two letters "bs" are displayed. The value of our variable %a% is not bbs.verybat.cn? How come only the second and third letters "bs" are displayed? After analyzing the result, we can easily see that %PATH:~10,5% is to display the value of the specified number of bits in the variable PATH!
Analyze `set var=%a:~1,2%`
The `set` command, the variable value `var`, `a` is the variable to be character-operated, "1" starts from the fewth bit of the variable "a", "2" displays several bits, and together it is to assign the value of the variable a from the first bit, and assign the last two bits to the variable var.
Well, it should be understood.
The other two syntaxes:
%PATH:~-10%
%PATH:~0,-2%
They also mean to display the value of the specified number of bits of the specified variable.
%PATH:~-10% Example:
@echo off
set a=bbs.verybat.cn
set var=%a:~-3%
echo %var%
pause
This is to assign the value of the last 3 bits of the variable a to the variable VAR.
Of course, we can also change it to this:
@echo off
set a=bbs.verybat.cn
set var=%a:~3%
echo %var%
pause
This is to assign the value of the first 3 bits of the variable a to the variable VAR.
%PATH:~0,-2% Example:
@echo off
set a=bbs.verybat.cn
set var=%a:~0,-3%
echo %var%
pause
After execution, we find that "bbs.verybat" is displayed, missing ".cn".
From the result analysis, it is easy to analyze that this is to display the value of the variable a from bit 0, and display the value of the bit of the total number of bits of the variable a minus 3 (the value of our variable a is bbs.verybat has 11 bits, 11-3=8), so it only displays the value from bit 0 to bit 8, and assigns it to the variable var.
If you change it to this:
@echo off
set a=bbs.verybat.cn
set var=%a:~2,-3%
echo %var%
pause
Then it will display the value from bit 2 to bit 8, and assign it to the variable var.
[ Last edited by 1112yuhua on 2007-9-12 at 01:29 PM ]
Recent Ratings for This Post
( 5 in total)
Click for details
| Rater | Score | Time |
|---|---|---|
| plp626 | +4 | 2008-01-29 21:44 |
| hy433124shc | +2 | 2008-02-22 16:19 |
| zhxy9804 | +2 | 2008-09-11 18:26 |
| ly2518644 | +1 | 2008-12-15 23:57 |
| 313885174 | +2 | 2008-12-16 23:09 |

