DOS的set命令用法(转)

chenlei posted @ 2016年3月25日 23:58 in Windows , 1166 阅读

http://blog.163.com/zxzx5200@126/blog/static/18612884200801632011512/

DOS中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 [variable=[string]]

SET /P variable=[promptString]

这两种语法外,还有如下几种语法:

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

 

  • 无匹配
  • 无匹配
Avatar_small
Share market holiday 说:
2022年8月01日 17:00

National Stock Exchange has got some specific timing, during which the trading will be scheduled and later there will not be an official trading session processed. Share market holiday There will be no trading possessed during the Saturday and Sunday along with some National Holidays declared by the Indian Government, and if anyone is trading, then they must be aware of these NSE holiday list.National Stock Exchange is open from Monday to Friday during the business hours to operate share market trading.There will be no trading possessed during the Saturday and Sunday along with some National Holidays declared by the Indian Government, and if anyone is trading, then they must be aware of these NSE holiday list.

Avatar_small
>1st Inter Model Pap 说:
2022年8月19日 21:08

Students taking the class 11 exams administered by the Uttarakhand Board may review the important model question paper from 2023 for better preparation. On the official website, you may obtain the 11th Important Model Question Paper for 2023 for the Science, Commerce, and Arts streams. However, the Board will shortly publish the condensed Important Model Question Paper 2023 for the upcoming academic year. 1st Inter Model Paper 2023 Students may verify the marking scheme and the Important Model Question Paper 2023 using this UK Board 11th Practice exam. The 11th Significant Model Question Paper from the Uttarakhand Board for 2023 also lists important themes for each subject, such as English, Economics, Geography, and Mathematics, unit by unit.

Avatar_small
fiyona cha 说:
2023年1月01日 15:37

The DOS set command is used to set environment variables. These variables can be used to control the behavior of programs and batch files. The set command can also be used to set the value of variables. The transfer Best CBD Topicals command is used to transfer the value of a variable from one environment to another.

Avatar_small
Khajane 2 Login 说:
2023年1月23日 15:48

K2 challan also referred to as Khajane 2, an integrated financial management system from the Government of Karnataka. The K2 has been brought into working with an aim to manage the financial business of the government. Khajane 2 Login It works to simplify the process of remittance of departments under government by bringing an option of anywhere-anytime payment options. Firstly every department under the government of Karnataka will have access to Khajane 2 which allows their customers to remit to the government through the easy payment links provided.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter