如果 IF 语句 returns 为真,如何设置新变量?
How to set new variable if the IF statement returns true?
我有一个创建特定变量的批处理代码。但是,我想添加一个 IF
语句,这样如果用户为第一个变量输入 "Technology",它就会创建另一个变量并询问其他信息。然后 python 脚本读取这些变量以进行进一步处理。
代码如下:
set constraint_type=1
set constraint_technology=1
set /p constraint_type="Enter constraint type (E.g. "Equipment" or "Technology"): "
IF /I !constraint_type!=="Technology" set /p constraint_technology="Enter Technology name: "
cmd /k python "script.py" %constraint_type% %constraint_technology%
@echo on
代码运行但不会提示用户输入 constraint_technology
变量。我错过了什么吗?
您可能没有启用延迟扩展
!constraint_type!
没有它就不会评估。同时删除引号,因为批处理不会这样做,所以比较总是错误的,因为用户不会在输入中添加引号。
更好的解决方法:使用标准环境。 var 定界符,适用于所有情况,
IF /I %constraint_type%==Technology
我有一个创建特定变量的批处理代码。但是,我想添加一个 IF
语句,这样如果用户为第一个变量输入 "Technology",它就会创建另一个变量并询问其他信息。然后 python 脚本读取这些变量以进行进一步处理。
代码如下:
set constraint_type=1
set constraint_technology=1
set /p constraint_type="Enter constraint type (E.g. "Equipment" or "Technology"): "
IF /I !constraint_type!=="Technology" set /p constraint_technology="Enter Technology name: "
cmd /k python "script.py" %constraint_type% %constraint_technology%
@echo on
代码运行但不会提示用户输入 constraint_technology
变量。我错过了什么吗?
您可能没有启用延迟扩展
!constraint_type!
没有它就不会评估。同时删除引号,因为批处理不会这样做,所以比较总是错误的,因为用户不会在输入中添加引号。
更好的解决方法:使用标准环境。 var 定界符,适用于所有情况,
IF /I %constraint_type%==Technology