Windows批处理文件:多个if条件

发布于 2024-11-08 18:51:05 字数 130 浏览 0 评论 0原文

有没有办法

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

骑趴 2024-11-15 18:51:05

事实证明“and”很简单——只是不是你期望的语法:
这3个例子就说明了这一点。

换句话说: If 1==1 AND 2==2 then echo "hello"

if 1==1 echo hello
hello

if 1==1 if 2==2 echo hello
hello

if 1==1 if 2==1 echo hello
(nothing was echoed)

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"

if 1==1 echo hello
hello

if 1==1 if 2==2 echo hello
hello

if 1==1 if 2==1 echo hello
(nothing was echoed)
葬﹪忆之殇 2024-11-15 18:51:05

实现逻辑或的一种方法是使用多个转到同一标签的条件。

if %1 == 1 goto :cond
if %1 == 2 goto :cond
goto :skip
:cond
someCommand
:skip

要测试集合成员资格,您可以使用 for 循环:

for %%i in (1 2 3 4 ... 20) do if %1 == %%i someCommand

请注意,== 是字符串相等运算符。 equ 是数字相等运算符。

One way to implement logical-or is to use multiple conditionals that goto the same label.

if %1 == 1 goto :cond
if %1 == 2 goto :cond
goto :skip
:cond
someCommand
:skip

To test for set membership, you could use a for-loop:

for %%i in (1 2 3 4 ... 20) do if %1 == %%i someCommand

Note that == is the string equality operator. equ is the numeric equality operator.

鸵鸟症 2024-11-15 18:51:05

我知道这已经过时了,但我只是想让你知道,这确实是可能的,不像之前的帖子所说的那样。基本上,您将两个 IF 命令捆绑为一个。

语法:IF 方程 (cmd if true)else command if false

尝试使用两个变量(执行 IF xx AND xx 语句)

set varone=1
set vartwo=2

if %varone% equ 1 (if %vartwo% equ 2 (echo TRUE)else echo FALSE)else echo FALSE

和一个变量(执行 OR 语句 - 请注意,您可以使用多个变量)变量)

if %a% equ 1 (echo pass)else if %a equ 2 (echo pass)else echo false

您可以用您的命令替换 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)

set varone=1
set vartwo=2

if %varone% equ 1 (if %vartwo% equ 2 (echo TRUE)else echo FALSE)else echo FALSE

with one Variable (to perform OR statement- note you can use more than one variable as well)

if %a% equ 1 (echo pass)else if %a equ 2 (echo pass)else echo false

You can substitute Echo pass/fail with your command

ペ泪落弦音 2024-11-15 18:51:05

游戏有点晚了,但仍然假设这是否可以帮助任何偶然发现这个问题的人。我这样做的方法是使用通过管道传输到 findstr 的 echo 组合,这样:

(echo ":1: :2:" | findstr /i ":%1:" 1>nul 2>nul) && (
    echo Input is either 1 or 2
)

由于 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:

(echo ":1: :2:" | findstr /i ":%1:" 1>nul 2>nul) && (
    echo Input is either 1 or 2
)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文