2017-01-09 58 views
1

我有创建某些变量的批处理代码。但是,我想添加一个IF语句,以便如果用户为第一个变量输入“Technology”,则会创建另一个变量并要求提供其他信息。一个python脚本然后读取这些变量进行进一步处理。如果IF语句返回true,如何设置新变量?

下面是代码:

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变量。我错过了什么吗?

+0

'IF/constraint_type == “技术”'! - >'IF/I “%constraint_type%” == “技术”'(正常''%扩展和报价,以避免空入境麻烦) – aschipfl

+0

@aschipfl - 真棒,感谢您的提示;) – Joseph

回答

1

你可能没有启用延迟扩展

!constraint_type!就没有它的评估。也删除引号,因为批量不会这样做,所以比较将始终为假,因为用户不会在引号中输入引号。

更好的解决方法:使用标准环境。 VAR分隔符,适用于所有的情况下,

IF /I %constraint_type%==Technology 
+0

感谢您的回答,但问题仍然存在。 – Joseph

+1

检查我的编辑。报价必须是问题。 –

+0

你是绝对正确的,现在效果很好。非常感谢您的回答:) – Joseph