多个条件的批处理文件语法?
Batch file syntax for multiple criteria?
是否可以让 IF 语句检查两个条件?还是必须是两个 IF 语句?
我想说"IF a user name is 'owen' or 'oiverson' then GOTO this..."
没有!但是有许多不同的解决方法。
试试这个:
set username=Owen
set found=no
if [%username%]==[Owen] set found=yes
if [%username%]==[oiverson] set found=yes
if %found%==yes goto :yes
goto :no
:yes
@echo Found user
:no
您可以像这样将两个 if
语句与 goto
一起使用:
rem assuming variable %user% holds the value to check:
if "%user%"=="owen" goto :CondTrue
if "%user%"=="oiverson" goto :CondTrue
:CondFalse
rem ... (none of the conditions is fulfilled)
goto :Continue
:CondTrue
rem ... (one or both conditions are met)
:Continue
rem ...
视具体情况而定。例如,如果你想检查一个用户名是否包含在一个名称列表中(似乎是这种情况),那么你可以使用这种方法:
setlocal EnableDelayedExpansion
set "validNames=/owen/oiverson/"
if "!validNames:/%userName%/=!" neq "%validNames%" (
echo "%userName%" is included in this list: "%validNames%"
)
当然,有效名称列表可能有任意数量 个名称。
如果您希望两个变量都具有特定的特定值(并且在所有变量上),请使用:
if "%user%+%version%" equ "expected_user+expected_ver" (
echo Both variables have the target values
)
您可以通过这种方式检查两个以上的变量。
前面说了,要看具体情况...
是否可以让 IF 语句检查两个条件?还是必须是两个 IF 语句?
我想说"IF a user name is 'owen' or 'oiverson' then GOTO this..."
没有!但是有许多不同的解决方法。 试试这个:
set username=Owen
set found=no
if [%username%]==[Owen] set found=yes
if [%username%]==[oiverson] set found=yes
if %found%==yes goto :yes
goto :no
:yes
@echo Found user
:no
您可以像这样将两个 if
语句与 goto
一起使用:
rem assuming variable %user% holds the value to check:
if "%user%"=="owen" goto :CondTrue
if "%user%"=="oiverson" goto :CondTrue
:CondFalse
rem ... (none of the conditions is fulfilled)
goto :Continue
:CondTrue
rem ... (one or both conditions are met)
:Continue
rem ...
视具体情况而定。例如,如果你想检查一个用户名是否包含在一个名称列表中(似乎是这种情况),那么你可以使用这种方法:
setlocal EnableDelayedExpansion
set "validNames=/owen/oiverson/"
if "!validNames:/%userName%/=!" neq "%validNames%" (
echo "%userName%" is included in this list: "%validNames%"
)
当然,有效名称列表可能有任意数量 个名称。
如果您希望两个变量都具有特定的特定值(并且在所有变量上),请使用:
if "%user%+%version%" equ "expected_user+expected_ver" (
echo Both variables have the target values
)
您可以通过这种方式检查两个以上的变量。
前面说了,要看具体情况...