我是非常新的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的语法是错误的。但我不知道它该去哪。有没有更好的方式来格式化三重如果?有办法去和我的如果吗?其他声明应该在哪里呢?
发布于 2015-04-22 16:55:24
您的代码有几个问题:
IF defined只能应用于环境变量,而不能应用于批处理文件参数。%1%构造是错误的,因为%1被替换为第一个参数(如果有的话),而第二个%只是被忽略。IF "%~1" equ "" echo Parameter 1 not given。%1和%2参数;只需要检查是否给出了%3。这样,您的代码可以简化为以下代码:。
IF "%~3" neq "" (
GOTO copyoutvariables
) ELSE (
GOTO parametererror
)发布于 2015-04-22 14:08:42
我想这是你的包袱,基于一些遥远的记忆。
尝试:
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.希望这能有所帮助。
发布于 2015-04-22 14:06:26
您需要在ELSE周围再加上几个括号。
IF defined %1% (
IF defined %2% (
IF defined %3% (
GOTO copyoutvariables
) ELSE (
GOTO parametererror
)
)
)https://stackoverflow.com/questions/29799909
复制相似问题