          『楼 主』:
非常不错set命令用法 (转载)(适合初学者)
使用 LLM 解释/回答一下
虽然用set /?也可得到set命令的用法,但是其翻译得实在不敢恭维,英文呢又不甚懂。几经周折在网上找到一篇介绍set命令的很不错的文章,作者在引用set /?帮助文件的同时加入了自己见解,做了通俗易懂的解释,非常适合像我这种菜鸟。
如果论坛已有同样或类似的帖子,就请版主把此贴删了。
set命令详解.对新手很有帮助
很久没发贴了,今天来写点讲BAT的新手教学贴!
在上一贴中我简单的介绍了一下SET设置自定义变量的作用,现在我来具体讲一下set的其他功
能.
先回顾一下他设置自定义变量的用法
例子:
@echo off
set var=我是值
echo %var%
pause
请看 set var=我是值 ,这就是BAT直接在批处理中设置变量的方法!
set 是命令 var是变量名 =号右边的"我是值"是变量的值
在批处理中我们要引用这个变就把var变量名用两个%(百分号)扩起来,如%var%
这种SET语法只能直接在BAT代码的提前赋予变量的值,有时候我们需要提供一个交互界面,让
用户自己输入变量的值,然后我们在来根据这个值来做相应操作,现在我就来说说这SET的这
种语法,只需要加一个"/P"参数就可以了!
例子:
@echo off
set /p var=请输入变量的值:
if %var% == 1 echo 您输入了 1 ~_~
pause
set /p 是命令语法 var是变量名 =号右边的"请输入变量的值: ",这个是提示语,不是变
量的值了!
运行后,我们在提示语后面直接输入1,就会显示一行您输入了 1 ~_~ ,输入别的就没有任何反
映!
好了,先回顾到这,现在讲SET其他功能
使用set /?查看SET的帮助我们发现SET除了我上面讲的
SET ]
SET /P variable=
这两种语法外,还有如下几种语法:
SET /A expression
环境变量替换已如下增强:
%PATH:str1=str2%
%PATH:~10,5%
%PATH:~-10%
%PATH:~0,-2%
这机种语法有什么用处呢?现在我们来一个个讲解他们!
SET /A expression
/A 命令行开关指定等号右边的字符串为被评估的数字表达式。该表达式
评估器很简单并以递减的优先权顺序支持下列操作:
() - 分组
! ~ - - 一元运算符
* / % - 算数运算符
+ - - 算数运算符
<< >> - 逻辑移位
& - 按位“与”
^ - 按位“异”
| - 按位“或”
= *= /= %= += -= - 赋值
&= ^= |= <<= >>=
, - 表达式分隔符
上面这些是系统帮助里的内容,看着是不是有点晕,没关系我来简单解释一下:
set的/A参数就是让SET可以支持数学符号进行加减等一些数学运算!
现在开始举例子介绍这些数学符号的用法:
看例子 这里的例子请直接在CMD下拷贝命令运行,不需要保存为BAT!
set /a var=1 + 1
set /a 语法, var变量名 1 + 1 数学式子
拷贝运行后会直接显示一个2,或者运行完后我们输入echo %var%,也是二,这就是
一个简单的加法运算!
set /a var=2 - 1 结果是多少呢?如果你看不到结果就echo %var%.....
set /a var=2 * 2 乘法运算
set /a var=2 / 2 除法运算
set /a var=(1+1) + (1+1) 结果等于4 看得懂吧!
set /a a=1+1,b=2+1,c=3+1 运行后会显示一个4,但我们用
echo %a% %b% %c%后看结果,会发现其他数学运算也有效果!,这就是"斗"号的
作用!
有时候我们需要直接在原变量进行加减操作就可以用这种语法
set /a var+=1 这样的语法对应原始语法就是set /a var = %var% + 1
都是一样的结果,在原变量的值上在进行数学运算,不过这样写简单一点
在来一个:
set /a var*=2
其他都这么用,只要帮助里有这个语法!
另外还有一些用逻辑或取余操作符,这些符号,按照上面的使用方法会报错的
比如我们在CMD里输入set /a var=1 & 1 "与运算",他并不会显示为1,而是报错,
为什么?对于这样的"逻辑或取余操作符",我们需要把他们用双引号引起来,看例子
set /a var= 1 "&" 1 这样结果就显示出来了,其他逻辑或取余操作符用法
set /a var= 1 "+" 1 异运算
set /a var= 1 "%" 1 取模运算
set /a var= 2 "<<" 2 次方运算
set /a var= 4 ">>" 2 这个不太记得数学里的叫法....
还有几个数学不太行,搞不清楚了....不列出来了,
这些符号也可以用&= ^= |= <<= >>= 这样的简单用法如
set /a var"&=" 1 等于set /a var = %var% "&" 1 注意引号
好符号说到这,现在说%PATH:str1=str2%
这个是替换变量值的内容,看例子
@echo off
set a= bbs.verybat.cn
echo 替换前的值: "%a%"
set var=%a: =%
echo 替换后的值: "%var%"
pause
对比一下,我们发现他把变量%a%的空格给替换掉了,从这个例子,我们就可以发现
%PATH:str1=str2%这个操作就是把变量%PATH%的里的str1全部用str2替换
比如我们把上面的例子改成这样
@echo off
set a=bbs.verybat.cn
echo 替换前的值: "%a%"
set var=%a:.=伤脑筋%
echo 替换后的值: "%var%"
pause
解释set var=%a:.=伤脑筋%
set命令 var变量名 字a是要进行字符替换的变量的值,"."为要替换的值,
"伤脑筋"为替换后的值!
执行后就会把变量%a%里面的"."全部替换为"伤脑筋"
这就是set的替换字符的很好的功能!先讲到这
%PATH:~10,5% 这个什么意思,看例子:
@echo off
set a=bbs.verybat.cn
set var=%a:~1,2%
echo %var%
pause
执行后,我们会发现只显示了"bs"两个字母,我们的变量%a%的值不是为bbs.verybat.cn吗
怎么只显示了第2个字母和第3个字母"bs",分析一结果我们就可以很容易看出
%PATH:~10,5%就是显示变量PATH里指定几位的值!
分析set var=%a:~1,2%
set命令 var变量值 a要进行字符操作的变量 "1"从变量"a"第几位开始显示 "2"显示几位
和起来就是把变量a的值从第一位开始,把后两位赋予给变量var
就样因该明白了吧~
其他两种语法
%PATH:~-10%
%PATH:~0,-2%
他们也是显示指定变量指定几位的值得的意思
%PATH:~-10% 例子
@echo off
set a=bbs.verybat.cn
set var=%a:~-3%
echo %var%
pause
这个就是把变量a倒数3位的值给变量VAR
当然我们也可以改成这样
@echo off
set a=bbs.verybat.cn
set var=%a:~3%
echo %var%
pause
这个就是把变量a的前3位的值给变量VAR
%PATH:~0,-2% 例子
@echo off
set a=bbs.verybat.cn
set var=%a:~0,-3%
echo %var%
pause
执行后,我们发现显示的是"bbs.verybat",少了".cn"
从结果分析,很容易分析出,这是把变量a的值从0位开始,
显示变量a总位数-3的位的值得(我们给变量a的的值bbs.verybat有11位,11-3=8),这样他就
只显示从第0位开始到第8位的值,并赋予给变量var
如果改成这样
@echo off
set a=bbs.verybat.cn
set var=%a:~2,-3%
echo %var%
pause
那么他就是显示从第2位开始到第8位的值,并赋予给变量var
Last edited by 1112yuhua on 2007-9-12 at 01:29 PM ]
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 ]
|