![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-09-29 00:28 |
47,813 topics / 349,918 posts / today 0 new / 48,278 members |
| DOS批处理 & 脚本技术(批处理室) » Very good set command usage (reprinted) (suitable for beginners) |
| Printable Version 7,534 / 16 |
| Floor1 1112yuhua | Posted 2007-08-01 16:50 |
| 初级用户 Posts 44 Credits 106 | |
|
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 ] |
|
| Floor2 taoty | Posted 2007-08-02 22:28 |
| 中级用户 Posts 112 Credits 275 | |
|
I've read the help file for this command countless times but still don't understand it. This article is very detailed, and I'm very grateful to the original poster for sharing.
|
|
| Floor3 dy2003310 | Posted 2007-08-04 12:36 |
| 初级用户 Posts 68 Credits 180 From China | |
|
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 take the value of the first 3 characters of variable a and assign it to variable VAR I tried this, and the result is var=.verybat.cn |
|
| Floor4 ccwan | Posted 2007-08-04 14:00 |
| 金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊 | |
|
Yes, set var=%a:~3% gets the string starting from the fourth character, and you can also understand it as removing the first three characters.
|
|
| Floor5 1112yuhua | Posted 2007-08-04 14:17 |
| 初级用户 Posts 44 Credits 106 | |
|
Re: dy2003310
Just to note, this article was not written by me; I'm not at that level yet. If there are any mistakes, please feel free to point them out. The line "set var=%a:~3%" should be changed to "set var=%a:~0,3%" which is to take the first three characters. Thanks to brother dy2003310 for the correction. [ Last edited by 1112yuhua on 2007-8-4 at 02:22 PM ] |
|
| Floor6 maskang | Posted 2007-08-07 17:33 |
| 新手上路 Posts 2 Credits 4 | |
|
I'm installing MSDOS 7.1 using a virtual machine, but I can't make it work according to the example, and there are some strange phenomena. Does it have something to do with the virtual machine? Have any friends encountered similar problems?
|
|
| Floor7 rqcwwkbg | Posted 2007-09-03 17:45 |
| 新手上路 Posts 8 Credits 15 | |
|
Nice, I'm searching for SET instructions. You just sent it. Really nice!!!!!
|
|
| Floor8 ruinedone | Posted 2007-09-10 09:15 |
| 新手上路 Posts 8 Credits 16 | |
|
Thanks to the LZ.
I wrote a batch script by myself, but it's all wrong. Now I understand a bit. |
|
| Floor9 bgty21 | Posted 2007-09-18 23:27 |
| 初级用户 Posts 24 Credits 29 | |
|
Thanks to the LZ! In the past, I always found the set command too difficult. I couldn't understand Microsoft's help files; they were written too professionally... Now there is such a easy-to-understand article, it's really a blessing for us noobs...
|
|
| Floor10 alfried | Posted 2007-10-05 00:20 |
| 初级用户 Posts 18 Credits 44 | |
|
Why is "used here instead of %????
|
|
| Floor11 cash0167 | Posted 2008-01-14 15:06 |
| 初级用户 Posts 12 Credits 23 | |
|
A few articles I can understand! Easy to understand, very good!
|
|
| Floor12 djmonkey1 | Posted 2008-01-16 16:22 |
| 初级用户 Posts 13 Credits 26 | |
|
Thank you very much for the LZ's sharing, the book doesn't explain as detailed as this.
|
|
| Floor13 hdq197611 | Posted 2008-01-17 23:02 |
| 新手上路 Posts 5 Credits 9 | |
|
Really grateful to the LZ, which made me understand a lot!
|
|
| Floor14 zbin929 | Posted 2008-01-25 07:38 |
| 新手上路 Posts 2 Credits 4 From 江西 | |
|
I'm new here. I took a look, the articles are well-written, I'll learn from them!
|
|
| Floor15 zfkms | Posted 2008-04-08 19:02 |
| 新手上路 Posts 7 Credits 15 | |
|
Recently, I've become obsessed with computer networks and have the urge to be a hacker. Hey, but I find there's so much to learn.
I'll start with batch processing first. Haha. I'm also a newbie. Please bear with me if what I say is incorrect. After reading this post, I've gained a good understanding of the set command (originally, I first looked at choice, but the XP system doesn't support this command now). Now, I'd like to make a few comments on this post. 1. In it, when replacing spaces and dots, it would be easier for beginners to understand if they were replaced with a certain letter. 2. I think the explanation of the following example in the post is wrong. %PATH:~0,-2% Example @echo off set a=bbs.verybat.cn set var=%a:~0,-3% echo %var% pause After execution, we find that it displays "bbs.verybat", missing ".cn" From the result analysis, it's easy to analyze that this is displaying the value from the 0th position of the variable a, and taking the value of the total number of bits of variable a minus 3. (The value of variable a we gave is bbs.verybat, which has 11 bits, 11-3=8). So it only displays the value from the 0th position to the 8th position and assigns it to variable var. It should be starting from the 0th position and removing the last 3 bits of variable a, and assigning the remaining characters to variable VAR. |
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |