DOS批处理文件 - 设置变量"若"块
问题描述:
下面的程序总是呼应机器XYZ不论该IF语句计算结果为TRUE或FALSE:
The following program always echoes "machine-xyz" irrespective of whether the "IF" statement evaluates to TRUE or FALSE:
@echo off
set dropLoc=machine-abc
if %computername% == "xyz" then (
set dropLoc=machine-xyz
)
echo %dropLoc%
我如何使这项工作?
How do I make this work?
答
您得到了 SET 语法正确的第一次,为什么你决定写别的东西第二时间是圆的?此外,您必须添加的比较两侧的引号。不像其他的脚本间preters,引号不是特殊批次间preTER。
You got the SET syntax right the first time, how come you decided to write something else the second time round? Also, you have to add the quotation marks on both sides of the comparison. Unlike in other script interpreters, quotation marks aren't special for the batch interpreter.
@echo off
rem Change this for testing, remove for production
set computername=xyz
set dropLoc=machine-abc
if "%computername%" == "xyz" (
set dropLoc=machine-xyz
)
echo %dropLoc%