Switch statement equivalent in Windows batch file

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

提交回复
热议问题