在 IF 语句中使用 OR WinXP 批处理脚本
有没有办法通过 IF 语句传递 OR ?
例如:
SET var=two
IF "%var%"=="one" OR "two" OR "three" ECHO The number is between zero and four.
Is there a way to pass an OR through an IF statement?
Such as:
SET var=two
IF "%var%"=="one" OR "two" OR "three" ECHO The number is between zero and four.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不。
如果您需要更结构化的方法:
No.
If you need a more structured approach:
请参阅 Windows 批处理中的重复问题 IF... OR IF...文件 其中以下由@Apostolos 提出的解决方案,
例如
我发现这是我的批处理脚本中最直接、最简单的用例。
See duplicate question IF... OR IF... in a windows batch file where following solution proposed by @Apostolos
e.g.
I found to be the most straight forward and simple to use case in my batch scripts.
游戏有点晚了,但仍然假设这是否可以帮助任何偶然发现这个问题的人。我这样做的方法是使用通过管道传输到 findstr 的 echo 组合,这样:
由于 findstr 是外部命令,因此我建议不要在可能会经历 1000 次迭代的循环中使用它。如果情况并非如此,这应该可以解决您尝试执行的操作,而不是使用多个 if。另外,“:”的选择没有什么特别的,只是使用一个不太可能是 var 变量值的一部分的分隔符。
感谢其他人指出另一个似乎有类似问题的链接,我也会在那里发布这个回复,以防万一有人偶然发现这个问题并且没有完全到达这里。
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 var variable.
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.