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-31 21:21
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Some doubts about the set command (solved) View 9,436 Replies 44
Original Poster Posted 2007-01-21 00:38 ·  中国 广东 广州 电信
初级用户
Credits 99
Posts 43
Joined 2007-01-12 23:05
19-year member
UID 76317
Gender Male
Status Offline
Learning batch processing, I learned the set command and was completely confused. I searched in the forum and there are some set-related ones, but after reading them, I still don't understand. I also read the built-in help, but I still don't get it. Does anyone have a better tutorial on set? You can also send a link. Thanks in advance.

[ Last edited by namejm on 2007-1-20 at 08:07 PM ]
Floor 2 Posted 2007-01-21 00:43 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Wherever not understand, put forward it. There must always be someone who can answer it, right?
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 3 Posted 2007-01-21 00:48 ·  中国 重庆 沙坪坝区 电信
新手上路
Credits 16
Posts 8
Joined 2007-01-20 11:09
19-year member
UID 77138
Gender Male
Status Offline
Let's say it so that we can also learn from it.
Floor 4 Posted 2007-01-21 01:16 ·  中国 广东 广州 电信
初级用户
Credits 99
Posts 43
Joined 2007-01-12 23:05
19-year member
UID 76317
Gender Male
Status Offline
Problem 1:
The first line `set a=` means initializing the variable `a` without giving it an initial value. Then `set/p a=请输入关键字……` is used to let the user input a value and assign it to variable `a`. The statement "The set command does not allow variables to contain equal signs" means that if you try to assign a value to a variable using the set command, the value should not contain the equal sign character.

Problem 2:
In `set "STARTPRGNAME=%STARTPRGNAME:"=%"`, `"=%"` is used to remove all double quotes from the variable `STARTPRGNAME`. The `/i` in `if /i "%STARTPRGNAME%" == "cmd" goto :cmd_exe` means case - insensitive comparison. The `:.bat` in `if not "%STARTPRGNAME:.bat=%" == "%STARTPRGNAME%" goto :cmd_or_bat` is used to remove the substring `.bat` from the variable `STARTPRGNAME`. The reason for enclosing `set "STARTPRGNAME=%STARTPRGNAME:"=%"` in quotes is to handle cases where the variable value might contain spaces. `%STARTPRGNAME%` is getting the value of the variable `STARTPRGNAME`, and `%1` is the first command - line argument, they are different in terms of their source (one is a variable, the other is a command - line argument).

Translated:
Problem 1:
The questions are all very basic, hope everyone has a little patience... Hehe.
Problem one:
set a=
set/p a=Please enter the keyword...
start http://www.baidu.com/s?wd=%a%
The built - in help is written like this: SET /P variable=
Why is there nothing on the right side of the equal sign in the first line? Then what is assigned to variable a exactly?
And there is such a sentence: "The set command does not allow variables to contain equal signs" How to understand it?

Problem two:
I saw such a post:
set "STARTPRGNAME=%STARTPRGNAME:"=%"
What does ":=" mean in the statement?

if /i "%STARTPRGNAME%" == "cmd" goto :cmd_exe
What does /i mean in the statement?

if not "%STARTPRGNAME:.bat=%" == "%STARTPRGNAME%" goto :cmd_or_bat
What does .bat mean in the statement?
In the first sentence: Why is the statement after set enclosed in quotes? What does it represent? Also, what does the %STARTPRGNAME on the right side of the equal sign mean? What is assigned to STARTPRGNAME on the left side of the equal sign?
In the second sentence: What is the situation of "%STARTPRGNAME%"? What is the difference between it and %1?
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
lokeymei +1 2008-11-08 12:59
Floor 5 Posted 2007-01-21 03:13 ·  中国 北京 联通
中级用户
★★
带走
Credits 435
Posts 88
Joined 2005-09-24 19:22
20-year member
UID 42793
Status Offline
HoHoHo~ I'm more suitable for answering such basic questions.

For question one, just try it yourself. The first sentence originally means to set variable a to empty, so there is naturally nothing on the right side of the equal sign. In fact, it's just deleting variable a. The second sentence means reading the user's input and assigning it to variable a. The sentence "" is a prompt for the user to see. That is, after you execute , the screen displays , and then you enter and press Enter, the value of variable a will be . You can understand it by adding a sentence . The statement that "" is easy to understand. Like the above a is the variable name, can you set a variable named ?

For question two:
You carefully check the help of and will see this sentence , which means replacing all characters or strings in variable with . The help also says that . For example, if the value of variable is , after executing , the value of variable is .
Thus, this sentence can be understood as deleting all quotes in variable and assigning the processed value back to . There are quotes because the character to be processed is a .

Continue. . .
in means case-insensitive. This is easy to understand, you are too lazy, building owner.
The next sentence is the same meaning as the above variable replacement. For example, assume the value of variable is , then ==, which may mean like this (I guess): if it is a.bat file, jump to here .

Answer the last line.
is to expand the value of variable , in fact, it is to read the variable. is just used to represent , because in batch processing, doubling can get one , and means the first parameter of the batch file. You can try creating a new batch file, such as , the file content is only one line , and then call it with the command line to see .

Typing is so tiring~~

[ Last edited by 0401 on 2007-1-21 at 03:18 AM ]
Recent Ratings for This Post ( 12 in total) Click for details
RaterScoreTime
redtek +7 2007-01-21 05:19
qzwqzw +7 2007-01-21 08:48
hangyug +2 2007-01-23 11:53
wyd531 +2 2007-01-26 03:42
txtrain +2 2007-02-09 22:06
oilio +2 2007-02-14 10:06
mtiankong +2 2007-04-18 09:13
b102588 +2 2007-07-11 21:13
liuyun20 +1 2007-10-20 17:33
regvip2008 +2 2008-01-15 16:40
zyz0304360 +1 2008-01-15 16:59
forkfan +1 2008-04-06 14:55
Floor 6 Posted 2007-01-21 04:46 ·  中国 广东 广州 天河区 电信
初级用户
Credits 99
Posts 43
Joined 2007-01-12 23:05
19-year member
UID 76317
Gender Male
Status Offline
Thanks to the above - mentioned person for the patient answer, you've worked hard... I understand question one, but you may not have understood my question two.
I'll say it again.
The statement "set "STARTPRGNAME=%STARTPRGNAME:"=%"": What does ":=%" mean?
The statement "if /i "%STARTPRGNAME%" == "cmd" goto :cmd_exe": What does /i mean?
The statement "if not "%STARTPRGNAME:.bat=%" == "%STARTPRGNAME%" goto :cmd_or_bat": What does .bat mean?
All of the above are the contents of a post I saw, and the questions are asked by the person who wrote this post, and my questions are below:
In the first sentence: Why is the statement after set enclosed in quotes? What does it represent? Also, what does the meaning of %STARTPRGNAME on the right side of the equal sign mean? What is assigned to STARTPRGNAME on the left side of the equal sign?
In the second sentence: What's the matter with "%STARTPRGNAME%"?
Especially: What's the difference between it ("%STARTPRGNAME%") and %%1? I didn't see the parameter in the form of "%STARTPRGNAME%" when learning if...
[ Last edited by hangyug on 2007 - 1 - 20 at 03:50 PM ]
Floor 7 Posted 2007-01-21 05:19 ·  中国 北京 朝阳区 联通
高级用户
★★
朦胧的世界
Credits 579
Posts 218
Joined 2006-10-24 04:29
19-year member
UID 67972
Status Offline
Two Great Secrets:

1. Make good use of the search function

2. Read more help information

.....

认识自己,降伏自己,改变自己
,才能改变别人!
Floor 8 Posted 2007-01-21 06:25 ·  中国 北京 联通
中级用户
★★
带走
Credits 435
Posts 88
Joined 2005-09-24 19:22
20-year member
UID 42793
Status Offline
To the original poster:

I understood your question two in the reply, but you didn't understand my answer.

Then I'll say it again.

Before saying that, I want to tell you that if you can try it yourself, I think you already know why.

My previous answer, I hope you can read it carefully again, I'm just giving an example here.

@echo off
set STARTPRGNAME=wInG
echo %%STARTPRGNAME%% variable value is %STARTPRGNAME%
set STARTPRGNAME=%STARTPRGNAME:G=gzero%
echo %%STARTPRGNAME%% variable after character replacement is %STARTPRGNAME%
if %STARTPRGNAME%==wingzero echo Does this sentence have output
if /i %STARTPRGNAME%==wingzero echo Does this one? It outputs, right.
pause>nul


Enough, original poster, read the above explanation several times. I can't express it any simpler.

BTW: Thanks to redtek for typing for me and adding points, heh heh.

[ Last edited by 0401 on 2007-1-21 at 06:30 AM ]
Floor 9 Posted 2007-01-21 08:04 ·  中国 广东 广州 天河区 电信
初级用户
Credits 99
Posts 43
Joined 2007-01-12 23:05
19-year member
UID 76317
Gender Male
Status Offline
Thank you, Brother 0401. It turns out you've answered both my question and the question in the post I cited. Thank you again for your reply...
Floor 10 Posted 2007-01-21 09:00 ·  中国 天津 电信
初级用户
Credits 97
Posts 43
Joined 2005-07-25 22:07
21-year member
UID 41055
Status Offline
I also want to thank such a warm - hearted and patient friend!
Floor 11 Posted 2007-01-23 11:36 ·  中国 江苏 苏州 电信
初级用户
Credits 25
Posts 11
Joined 2007-01-23 10:33
19-year member
UID 77427
Gender Male
Status Offline
Learned quite a bit, heh heh, continue...
Floor 12 Posted 2007-01-24 05:28 ·  中国 广东 广州 海珠区 电信
初级用户
Credits 68
Posts 32
Joined 2007-01-24 04:00
19-year member
UID 77490
Gender Male
From 广东潮州
Status Offline
It is suggested that if you look at several more examples, you will find it very easy to understand.

Also, you need to pay attention that the current selection command is different from the old DOS ones.
Floor 13 Posted 2007-02-08 09:55 ·  中国 山东 德州 电信
初级用户
Credits 24
Posts 11
Joined 2007-01-13 05:26
19-year member
UID 76357
Gender Male
Status Offline
It's explained very detailedly, so that we newcomers can understand. Thanks to brother 0401 for his patience and meticulousness!
Floor 14 Posted 2007-02-08 11:18 ·  中国 吉林 四平 联通
高级用户
★★★
Credits 859
Posts 413
Joined 2006-08-14 21:55
19-year member
UID 60532
Status Offline
The explanation about set is very detailed in the 5th floor, and there are examples. A bit better looking
Floor 15 Posted 2007-02-14 06:09 ·  中国 河南 郑州 联通
初级用户
Credits 88
Posts 40
Joined 2006-12-26 07:32
19-year member
UID 74591
Gender Male
Status Offline
What the person on the 5th floor said is very clear. We beginners can understand it at a glance
Forum Jump: