Switch statement equivalent in Windows batch file

前端 未结 9 1115
無奈伤痛
無奈伤痛 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.

提交回复
热议问题