Switch statement equivalent in Windows batch file

前端 未结 9 1046
無奈伤痛
無奈伤痛 2020-12-03 00:40

I wonder if there is a simple way to branch execution in a Windows batch file depending on the value of one single expression. Something akin to switch/case blocks in C, C++

相关标签:
9条回答
  • 2020-12-03 01:13

    I guess all other options would be more cryptic. For those who like readable and non-cryptic code:

    IF        "%ID%"=="0" (
        REM do something
    
    ) ELSE IF "%ID%"=="1" (
        REM do something else
    
    ) ELSE IF "%ID%"=="2" (
        REM do another thing
    
    ) ELSE (
        REM default case...
    )
    

    It's like an anecdote:

    Magician: Put the egg under the hat, do the magic passes ... Remove the hat and ... get the same egg but in the side view ...

    The IF ELSE solution isn't that bad. It's almost as good as python's if elif else. More cryptic 'eggs' can be found here.

    0 讨论(0)
  • 2020-12-03 01:15

    I searched switch / case in batch files today and stumbled upon this. I used this solution and extended it with a goto exit.

    IF "%1"=="red" echo "one selected" & goto exit
    IF "%1"=="two" echo "two selected" & goto exit
    ...
    
    echo "Options: [one | two | ...]
    
    :exit 
    

    Which brings in the default state (echo line) and no extra if's when the choice is found.

    0 讨论(0)
  • 2020-12-03 01:16

    Compact form for short commands (no 'echo'):

    IF "%ID%"=="0" ( ... & ... & ... ) ELSE ^
    IF "%ID%"=="1" ( ... ) ELSE ^
    IF "%ID%"=="2" ( ... ) ELSE ^
    REM default case...
    

    After ^ must be an immediate line end, no spaces.

    0 讨论(0)
  • 2020-12-03 01:18

    Try by this way. To perform some list of operations like

    1. Switch case has been used.
    2. Checking the conditional statements.
    3. Invoking the function with more than two arguments.

     @echo off
     :Start2 
        cls
        goto Start
        :Start
        echo --------------------------------------
        echo     Welcome to the Shortcut tool     
        echo --------------------------------------            
        echo Choose from the list given below:
        echo [1] 2017
        echo [2] 2018
        echo [3] Task
    
        set /a one=1
        set /a two=2
        set /a three=3
        set /a four=4
        set input=
        set /p input= Enter your choice:
        if %input% equ %one% goto Z if NOT goto Start2
        if %input% equ %two% goto X if NOT goto Start2
        if %input% equ %three% goto C if NOT goto Start2
        if %input% geq %four% goto N
    
        :Z
        cls
        echo You have selected year : 2017
        set year=2017
        echo %year%
        call:branches year
    
        pause
        exit
    
        :X
        cls
        echo You have selected year : 2018
        set year=2018
        echo %year%
        call:branches year
        pause
        exit
    
        :C  
        cls
        echo You have selected Task
        call:Task
        pause
        exit
    
        :N
        cls
        echo Invalid Selection! Try again
        pause
        goto :start2
    
    
    
    
        :branches
        cls
        echo Choose from the list of Branches given below:
        echo [1] January
        echo [2] Feburary
        echo [3] March
        SETLOCAL
        set /a "Number1=%~1"
        set input=
        set /p input= Enter your choice:
        set /a b=0
        set /a bd=3
        set /a bdd=4
        if %input% equ %b% goto N
        if %input% leq %bd% call:Z1 Number1,input if NOT goto Start2
        if %input% geq %bdd% goto N
    
    
    
        :Z1
        cls
        SETLOCAL
        set /a "Number1=%~1"
        echo year = %Number1%
        set /a "Number2=%~2"
        echo branch = %Number2%
        call:operation Number1,Number2
        pause
        GOTO :EOF
    
    
        :operation
        cls
        echo Choose from the list of Operation given below:
        echo [1] UB
        echo [3] B
        echo [4] C
        echo [5] l
        echo [6] R
        echo [7] JT
        echo [8] CT
        echo [9] JT
        SETLOCAL
        set /a "year=%~1"
        echo Your have selected year = %year%
        set /a "month=%~2"
        echo You have selected Branch = %month%
        set operation=
        set /p operation= Enter your choice:
        set /a b=0
        set /a bd=9
        set /a bdd=10
        if %input% equ %b% goto N
        if %operation% leq %bd% goto :switch-case-N-%operation% if NOT goto Start2
        if %input% geq %bdd% goto N
    
    
    
    
        :switch-case-N-1
        echo Januray
        echo %year%,%month%,%operation%
        goto :switch-case-end
    
        :switch-case-N-2
        echo Feburary
        echo %year%,%month%,%operation%
        goto :switch-case-end
    
        :switch-case-N-3
        echo march
        echo %year%,%month%,%operation%
        goto :switch-case-end
    
        :switch-case-end
           echo Task Completed
           pause
           exit
           goto :start2
    
    
    
    
    
    
        :Task
        cls
        echo Choose from the list of Operation given below:
        echo [1] UB
        echo [3] B
        echo [4] C
        echo [5] l
        echo [6] R
        echo [7] JT
        echo [8] CT
        echo [9] JT
        SETLOCAL
        set operation=
        set /p operation= Enter your choice:
        set /a b=0
        set /a bd=9
        set /a bdd=10
        if %input% equ %b% goto N
        if %operation% leq %bd% goto :switch-case-N-%operation% if NOT goto Start2
        if %input% geq %bdd% goto N
    
    
    
    
        :switch-case-N-1
        echo Januray
        echo %operation%
        goto :switch-case-end
    
        :switch-case-N-2
        echo Feburary
        echo %year%,%month%,%operation%
        goto :switch-case-end
    
        :switch-case-N-3
        echo march
        echo %year%,%month%,%operation%
        goto :switch-case-end
    
        :switch-case-end
           echo Task Completed
           pause
           exit
           goto :start2
    
    0 讨论(0)
  • 2020-12-03 01:19

    If if is not working you use:

    :switch case %n%=1 
    statements;
    goto :switch case end
    etc..
    

    http://lallouslab.net/2016/12/21/batchography-switch-case/

    0 讨论(0)
  • 2020-12-03 01:26

    It might be a bit late, but this does it:

    set "case1=operation1"
    set "case2=operation2"
    set "case3=operation3"
    
    setlocal EnableDelayedExpansion
    !%switch%!
    endlocal
    

    %switch% gets replaced before line execution. Serious downsides:

    • You override the case variables
    • It needs DelayedExpansion

    Might eventually be usefull in some cases.

    0 讨论(0)
提交回复
热议问题