如何在包含多个IF语句的.bat文件中使用ELSE语句?

时间:2017-01-24 19:31:46

标签: windows batch-file if-statement cmd

我正在编写Windows批处理文件以启用,禁用或手动执行Windows服务。

如果您有多个IF选项,您将如何使用ELSE语句?

例如:

if /I "%c%" EQU "A"  goto :automatic
if /I "%c%" EQU "M"  goto :manual
if /I "%c%" EQU "D"  goto :disabled

我需要一个ELSE语句将脚本发送到ECHO行,如果用户输入 A M D

感谢您的帮助。

4 个答案:

答案 0 :(得分:3)

Ken White已经向您展示了his answer中的完美解决方案。

我想告诉你,如果你真的想要使用else条款,它会有多复杂 让我们坚持以下代码:

if /I "%c%" EQU "A"  goto :automatic
if /I "%c%" EQU "M"  goto :manual
if /I "%c%" EQU "D"  goto :disabled
echo Wrong user input!
  1. 单独的if / else声明:

    括号是强制性的; :message块可以在脚本的其他位置:

    if /I "%c%" EQU "A"  goto :automatic
    if /I "%c%" EQU "M"  goto :manual
    if /I "%c%" EQU "D" (goto :disabled) else goto :message
    
    :message
    echo Wrong user input!
    
  2. 嵌套if / else声明:

    if /I "%c%" EQU "A" (goto :automatic) else (
        if /I "%c%" EQU "M" (goto :manual) else (
            if /I "%c%" EQU "D" (goto :disabled) else (
                echo Wrong user input!
            )
        )
    )
    

    或以不同的方式撰写:

    if /I "%c%" EQU "A" (
        goto :automatic
    ) else if /I "%c%" EQU "M" (
        goto :manual
    ) else if /I "%c%" EQU "D" (
        goto :disabled
    ) else (
        echo Wrong user input!
    )
    

    或更紧凑:

    if /I "%c%" EQU "A" (goto :automatic
    ) else if /I "%c%" EQU "M" (goto :manual
    ) else if /I "%c%" EQU "D" (goto :disabled
    ) else echo Wrong user input!
    
  3. 正如您所看到的,Ken White的解决方案仍然是最简单,最易读的解决方案。

    顺便说一下,Squashman提出了一个非常好的选择,即choice command

    choice /C AMD /N /M "Select mode - [A]utomatic, [M]anual or [D]isabled: "
    if ErrorLevel 3 goto :disabled
    if ErrorLevel 2 goto :manual
    if ErrorLevel 1 goto :automatic
    goto :eof
    

答案 1 :(得分:1)

只添加另一个没有条件的goto就可以了:

if /I "%c%" EQU "A"  goto :automatic
if /I "%c%" EQU "M"  goto :manual
if /I "%c%" EQU "D"  goto :disabled
goto :ErrorInfo

如果前三个中没有一个是可接受的,则代码通过落到最后一个,如果输入了不可接受的值(或者根本没有输入值),则会将它们带到您要执行的任何代码中)。

答案 2 :(得分:0)

如果您将字母附加到标签前缀并检查给定集合,则可以放宽评估单字母用户输入:

@Echo off
:Cont
Set MSG="Press [A]utomatic, [M]anual or [D]isabled :"
Set Choices=AMD
Set /P c=%MSG%
Echo:%Choices%|Find /i "%c:~0,1%" >NUL 2>&1 && Goto :Choice%c% || Goto :BadChoice

:Badchoice
Echo %c% is a bad choice
Goto :Cont

:ChoiceA
Echo ChoiceA
Goto :Cont

:ChoiceM
Echo ChoiceM
Goto :Cont

:ChoiceD
Echo ChoiceD
Goto :Cont

或者非常类似于将Squashman的建议与choice.exe相结合(这里也是第一个字母的10秒超时):

@Echo off&Setlocal EnableDelayedExpansion
:Cont
Set MSG="Press [A]utomatic, [M]anual or [D]isabled :"
Set Choices=AMD
CHOICE /C %Choices% /T 10 /D %Choices:~0,1% /M %MSG%
Set /A _=%Errorlevel% - 1
If %_% lss 255 Goto :Choice!Choices:~%_%,1!

:Badchoice
Echo %c% is a bad choice
Goto :Cont

:ChoiceA
Echo ChoiceA
Goto :Cont

:ChoiceM
Echo ChoiceM
Goto :Cont

:ChoiceD
Echo ChoiceD
Goto :Cont

答案 3 :(得分:0)

我认为这是最简单的方法:

setlocal EnableDelayedExpansion
set "options=AMD"


if "!options:%c%=!" neq "%options%" (
   goto :option-%c%
) else (
   goto badOption
)


:option-A automatic
echo Automatic
goto continue

:option-M manual
echo Manual
goto continue

:option-D disabled
echo Disabled
goto continue

首先,定义具有有效选项的字符串。然后,if命令检查%c%是否是这样的选项之一;如果答案为是,则goto :option-%c%命令直接跳转到所需的标签,而不使用任何其他if。这种方法非常有效,特别是如果有很多不同的选择。如果您想添加更多选项,只需在字符串中添加字母,其他if命令。