在 IF 语句中使用 OR WinXP 批处理脚本

发布于 2024-12-02 15:47:14 字数 157 浏览 2 评论 0原文

有没有办法通过 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 技术交流群。

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

发布评论

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

评论(3

回眸一遍 2024-12-09 15:47:14

不。

if "%var%"=="one" goto foo
if "%var%"=="two" goto foo
if "%var%"=="three" goto foo
goto afterfoo
:foo
echo The number is between one and three (technically incorrect, since it includes the end points and thus is not between).
:afterfoo

如果您需要更结构化的方法:

if "%var%"=="one" set OneToThree=1
if "%var%"=="two" set OneToThree=1
if "%var%"=="three" set OneToThree=1
if defined OneToThree (
    echo Foo
) else (
    rem something
)

No.

if "%var%"=="one" goto foo
if "%var%"=="two" goto foo
if "%var%"=="three" goto foo
goto afterfoo
:foo
echo The number is between one and three (technically incorrect, since it includes the end points and thus is not between).
:afterfoo

If you need a more structured approach:

if "%var%"=="one" set OneToThree=1
if "%var%"=="two" set OneToThree=1
if "%var%"=="three" set OneToThree=1
if defined OneToThree (
    echo Foo
) else (
    rem something
)
扶醉桌前 2024-12-09 15:47:14

请参阅 Windows 批处理中的重复问题 IF... OR IF...文件 其中以下由@Apostolos 提出的解决方案,

FOR %%a in (item1 item2 ...) DO IF {condition_involving_%%a} {execute_command}  

例如

FOR %%a in (1 2) DO IF %var%==%%a ECHO TRUE

我发现这是我的批处理脚本中最直接、最简单的用例。

See duplicate question IF... OR IF... in a windows batch file where following solution proposed by @Apostolos

FOR %%a in (item1 item2 ...) DO IF {condition_involving_%%a} {execute_command}  

e.g.

FOR %%a in (1 2) DO IF %var%==%%a ECHO TRUE

I found to be the most straight forward and simple to use case in my batch scripts.

帅的被狗咬 2024-12-09 15:47:14

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

(echo ":one: :two: :three:" | findstr /i ":%var%:" 1>nul 2>nul) && (
    echo The number is between zero and four
)

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

(echo ":one: :two: :three:" | findstr /i ":%var%:" 1>nul 2>nul) && (
    echo The number is between zero and four
)

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.

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