1. SET命令的使用格式:
SET [variable=[string]]
variable Specifies the environment-variable name.
string Specifies a series of characters to assign to the variable.
SET /A expression
SET /P variable=[promptString]
2. 表达式等号两端切勿多加空格或者引号:
SET a = 1
被定义的变量实际是"a "而非"a";其对应的值是" 1"而非"1"。
3. /A 用来标识等号右边的值是数值。因此,请注意进制问题:
0x开头的是十六进制。
0开头的是八进制。 (错:SET /A num=08)
4. 要注意延迟环境变量扩展——默认关闭,在IF和FOR中对环境变量赋值不会立即生效。
set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "%VAR%" == "after" @echo If you see this, it worked
)
上面的代码永远不会输出"If you see this, it worked",出了if,%VAR%才能得到一个新值。
可以开启延迟环境变量扩展:
SETLOCAL ENABLEDELAYEDEXPANSION
set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "!VAR!" == "after" @echo If you see this, it worked
)
取变量的值时,用"!"代替"%"。