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

Mec*_*ash 9 windows conditional cmd if-statement batch-file

有没有办法通过IF语句传递OR?

如:

SET var=two
IF "%var%"=="one" OR "two" OR "three" ECHO The number is between zero and four.
Run Code Online (Sandbox Code Playgroud)

Joe*_*oey 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
Run Code Online (Sandbox Code Playgroud)

如果您需要更有条理的方法:

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
)
Run Code Online (Sandbox Code Playgroud)

  • 您也可以使用这种方法:对于%% a in(一二三),如果"%var%"=="%% a"goto foo (3认同)