China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-05 05:59
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Very good set command usage (reprinted) (suitable for beginners) View 7,050 Replies 16
Original Poster Posted 2007-08-01 16:50 ·  中国 陕西 西安 电信
初级用户
Credits 106
Posts 44
Joined 2007-06-01 22:25
19-year member
UID 90001
Gender Male
Status Offline
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 ]
Recent Ratings for This Post ( 5 in total) Click for details
RaterScoreTime
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
Floor 2 Posted 2007-08-02 22:28 ·  中国 江苏 连云港 电信
中级用户
★★
传说中的菜鸟
Credits 275
Posts 112
Joined 2005-04-22 00:00
21-year member
UID 38486
Gender Male
Status Offline
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.
Floor 3 Posted 2007-08-04 12:36 ·  中国 广东 广州 天河区 电信
初级用户
★★
Credits 180
Posts 68
Joined 2006-12-21 23:44
19-year member
UID 74192
Gender Male
From China
Status Offline
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
Floor 4 Posted 2007-08-04 14:00 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
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.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 5 Posted 2007-08-04 14:17 ·  中国 陕西 西安 电信
初级用户
Credits 106
Posts 44
Joined 2007-06-01 22:25
19-year member
UID 90001
Gender Male
Status Offline
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 ]
Floor 6 Posted 2007-08-07 17:33 ·  中国 广东 广州 白云区 电信
新手上路
Credits 4
Posts 2
Joined 2007-08-01 12:53
18-year member
UID 94423
Gender Male
Status Offline
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?
Floor 7 Posted 2007-09-03 17:45 ·  中国 广东 江门 电信
新手上路
Credits 15
Posts 8
Joined 2007-08-22 22:27
18-year member
UID 95643
Gender Male
Status Offline
Nice, I'm searching for SET instructions. You just sent it. Really nice!!!!!
Floor 8 Posted 2007-09-10 09:15 ·  中国 河南 郑州 联通
新手上路
Credits 16
Posts 8
Joined 2007-09-08 18:37
18-year member
UID 96880
Gender Male
Status Offline
Thanks to the LZ.
I wrote a batch script by myself, but it's all wrong. Now I understand a bit.
Floor 9 Posted 2007-09-18 23:27 ·  中国 福建 泉州 电信
初级用户
Credits 29
Posts 24
Joined 2007-05-10 21:53
19-year member
UID 88110
Gender Male
Status Offline
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...
Floor 10 Posted 2007-10-05 00:20 ·  中国 河南 郑州 电信
初级用户
Credits 44
Posts 18
Joined 2007-09-20 21:54
18-year member
UID 97915
Gender Male
Status Offline
Why is "used here instead of %????
Floor 11 Posted 2008-01-14 15:06 ·  中国 湖南 益阳 电信
初级用户
Credits 23
Posts 12
Joined 2008-01-12 12:52
18-year member
UID 108377
Gender Male
Status Offline
A few articles I can understand! Easy to understand, very good!
Floor 12 Posted 2008-01-16 16:22 ·  中国 云南 昆明 五华区 电信
初级用户
Credits 26
Posts 13
Joined 2007-08-12 15:38
18-year member
UID 95009
Gender Male
Status Offline
Thank you very much for the LZ's sharing, the book doesn't explain as detailed as this.
Floor 13 Posted 2008-01-17 23:02 ·  中国 河北 秦皇岛 电信
新手上路
Credits 9
Posts 5
Joined 2008-01-17 08:46
18-year member
UID 108814
Gender Male
Status Offline
Really grateful to the LZ, which made me understand a lot!
Floor 14 Posted 2008-01-25 07:38 ·  中国 福建 厦门 电信
新手上路
Credits 4
Posts 2
Joined 2008-01-22 02:07
18-year member
UID 109222
Gender Male
From 江西
Status Offline
I'm new here. I took a look, the articles are well-written, I'll learn from them!
Floor 15 Posted 2008-04-08 19:02 ·  中国 北京 联通
新手上路
Credits 15
Posts 7
Joined 2007-07-25 19:53
18-year member
UID 94030
Gender Male
Status Offline
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.
Forum Jump: