Windows 批处理文件中的三重 IF 语句?如果和如果和如果?其他错误
Triple IF Statement in Windows Batch File? IF and IF and IF? Else Errors
我对 Windows 批处理文件非常陌生,很抱歉,如果这很明显。
基本上,在批处理文件中,我需要一个三重 IF 语句来检查是否在命令行上给出了 3 个参数。如果它们在那里,它使用 GOTO 来执行更多代码。如果它没有全部三个,那么它会回显错误。
这是我目前使用的代码,它不起作用
IF defined %1% (
IF defined %2% (
IF defined %3% (
GOTO copyoutvariables
ELSE GOTO parametererror
)
)
)
:parametererror
Echo You did not enter the right amount of parameters.
:copyoutvariables
Echo Irrelevant Code goes here.
如果我输入三个参数,那么它会直接进入 :parametererror
。
我认为 ELSE
的语法有误。我真的不知道它应该去哪里。
有没有更好的方法来格式化三重 IF?
有没有办法和我的 IF?
ELSE 语句应该放在哪里?
ELSE
.
两边还需要几个括号
IF defined %1% (
IF defined %2% (
IF defined %3% (
GOTO copyoutvariables
) ELSE (
GOTO parametererror
)
)
)
我认为这是你的包围,基于一些遥远的记忆。
尝试:
IF defined %1% IF defined %2% IF defined %3%
(
GOTO :copyoutvariables
)
ELSE
(
GOTO :parametererror
)
:parametererror
Echo You did not enter the right amount of parameters.
:copyoutvariables
Echo Irrelevant Code goes here.
希望对您有所帮助。
您的代码有几个问题:
IF defined
只能应用于环境变量,不能应用于批处理文件参数。
- 您的
%1%
构造是错误的,因为 %1
被第一个参数(如果有)替换,第二个 %
被忽略。
- 检查参数是否给定的正确方法是:
IF "%~1" equ "" echo Parameter 1 not given
.
- 您不需要检查是否同时提供了
%1
和 %2
参数;只是为了检查 %3
是否给出。这样,您的代码可能会减少到这个:
.
IF "%~3" neq "" (
GOTO copyoutvariables
) ELSE (
GOTO parametererror
)
我对 Windows 批处理文件非常陌生,很抱歉,如果这很明显。
基本上,在批处理文件中,我需要一个三重 IF 语句来检查是否在命令行上给出了 3 个参数。如果它们在那里,它使用 GOTO 来执行更多代码。如果它没有全部三个,那么它会回显错误。
这是我目前使用的代码,它不起作用
IF defined %1% (
IF defined %2% (
IF defined %3% (
GOTO copyoutvariables
ELSE GOTO parametererror
)
)
)
:parametererror
Echo You did not enter the right amount of parameters.
:copyoutvariables
Echo Irrelevant Code goes here.
如果我输入三个参数,那么它会直接进入 :parametererror
。
我认为 ELSE
的语法有误。我真的不知道它应该去哪里。
有没有更好的方法来格式化三重 IF?
有没有办法和我的 IF?
ELSE 语句应该放在哪里?
ELSE
.
IF defined %1% (
IF defined %2% (
IF defined %3% (
GOTO copyoutvariables
) ELSE (
GOTO parametererror
)
)
)
我认为这是你的包围,基于一些遥远的记忆。
尝试:
IF defined %1% IF defined %2% IF defined %3%
(
GOTO :copyoutvariables
)
ELSE
(
GOTO :parametererror
)
:parametererror
Echo You did not enter the right amount of parameters.
:copyoutvariables
Echo Irrelevant Code goes here.
希望对您有所帮助。
您的代码有几个问题:
IF defined
只能应用于环境变量,不能应用于批处理文件参数。- 您的
%1%
构造是错误的,因为%1
被第一个参数(如果有)替换,第二个%
被忽略。 - 检查参数是否给定的正确方法是:
IF "%~1" equ "" echo Parameter 1 not given
. - 您不需要检查是否同时提供了
%1
和%2
参数;只是为了检查%3
是否给出。这样,您的代码可能会减少到这个:
.
IF "%~3" neq "" (
GOTO copyoutvariables
) ELSE (
GOTO parametererror
)