Switch statement equivalent in Windows batch file

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

提交回复
热议问题