Windows批处理文件:多个if条件
有没有办法
if %1 == 1 or %1 == 2
在批处理文件中说类似的话?或者,如果我可以指定一组候选值,例如
if %1 in [1, 2, 3, 4, ... 20]
Is there a way to say something like
if %1 == 1 or %1 == 2
in a batch file? Or, even better, if I could specify a set of candidate values like
if %1 in [1, 2, 3, 4, ... 20]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
事实证明“and”很简单——只是不是你期望的语法:
这3个例子就说明了这一点。
换句话说: If 1==1 AND 2==2 then echo "hello"
The "and" turns out to be easy -- just not the syntax you expect:
These 3 examples illustrate it.
In words: If 1==1 AND 2==2 Then echo "hello"
实现逻辑或的一种方法是使用多个
转到
同一标签的条件。要测试集合成员资格,您可以使用 for 循环:
请注意,
==
是字符串相等运算符。equ
是数字相等运算符。One way to implement logical-or is to use multiple conditionals that
goto
the same label.To test for set membership, you could use a for-loop:
Note that
==
is the string equality operator.equ
is the numeric equality operator.我知道这已经过时了,但我只是想让你知道,这确实是可能的,不像之前的帖子所说的那样。基本上,您将两个 IF 命令捆绑为一个。
语法:
IF 方程 (cmd if true)else command if false
尝试使用两个变量(执行 IF xx AND xx 语句)
和一个变量(执行 OR 语句 - 请注意,您可以使用多个变量)变量)
您可以用您的命令替换 Echo pass/fail
I know this is old, but I just wanted to let you know, that it indeed is possible, unlike the previous posts said. Basically you are tying two IF commands into one.
Syntax:
IF equation (cmd if true)else command if false
Try this for two variables (to perform IF xx AND xx statement)
with one Variable (to perform OR statement- note you can use more than one variable as well)
You can substitute Echo pass/fail with your command
游戏有点晚了,但仍然假设这是否可以帮助任何偶然发现这个问题的人。我这样做的方法是使用通过管道传输到 findstr 的 echo 组合,这样:
由于 findstr 是外部命令,因此我建议不要在可能会经历 1000 次迭代的循环中使用它。如果情况并非如此,这应该可以解决您尝试执行的操作,而不是使用多个 if。另外,“:”的选择没有什么特别的,只需使用一个不太可能是 %1 中值的一部分的分隔符即可。
感谢其他人指出另一个似乎有类似问题的链接,我也会在那里发布这个回复,以防万一有人偶然发现这个问题并且没有完全到达这里。
A bit late in the game, but nevertheless assuming if this might help anyone stumbling upon the question. The way I do this is using a combination of echo piped to findstr, this way:
Since findstr is an external command, I recommend not using this inside a loop which may go through 1000's of iterations. If that is not the case, this should solve what you are attempting to do instead of using multiple ifs. Also, there is nothing special in the choice of ":", just use a delimiter which is unlikely to be part of the value in the %1.
Thanks to the rest of folks on pointing to another link which appears to have similar question, I will post this response there as well, just in case someone stumbles upon that question and doesn't quite reach here.