Search code examples
batch-filecmdmenubatch-choice

All option in choice command


I am wondering if there is a nice and clean way to add a "select all choices" when building a choice menu in batch.

Currently I have the choice setup as clear item 1, clear item 2, clear item 3 (clear all), exit, and a timeout that the user cannot see.

If I have to I will just re-add all my code to the "clear all" area. I was hoping to see if there was a way to just have the "clear all" use the defined 1 and 2 and then go back to :start like everything else.

Answer: I took Aacini's idea of the "set option" and came up with an even simpler answer. I Changed "GOTO :start"to"GOTO :ClearCache" for the "Clear All Options". Then I added a "IF %ERRORLEVEL% neq 3 GOTO :start" and after that a "GOTO :ClearCredentials". This allowed me to keep less lines than the set option and I didn't have to move my code around to have it pass to the next process.

This should allow for multiple but different clear all options for future items.

@ECHO OFF
:start
ECHO 1. Clear IE, Chrome, Temp Cache
ECHO 2. Clear Credentials in IE, Chrome, and Windows
ECHO 3. Clear All Options
ECHO 4. Exit
CHOICE /N /C:12345 /T 15 /D 5 /M "Type the number to choose an option."
IF %ERRORLEVEL%==5 GOTO TIMEOUT
IF %ERRORLEVEL%==4 GOTO Exit
IF %ERRORLEVEL%==3 GOTO ClearAllOptions
IF %ERRORLEVEL%==2 GOTO ClearCredentials
IF %ERRORLEVEL%==1 GOTO ClearCache
GOTO :start

:ClearCache
ECHO Clearing Cache...
<code here>
pause
cls
IF %ERRORLEVEL% neq 3 GOTO :start
GOTO :ClearCredentials


REM ===-----------------------

:ClearCredentials
ECHO Clearing Credentials
<code here>
pause
cls
GOTO :start

REM ===-----------------------

:ClearAllOptions
ECHO Clearing All Options...
pause
cls
GOTO :ClearCache
pause

REM ===-----------------------

:Exit
ECHO Exiting...
<code here>
pause
EXIT

REM ===-----------------------

:TIMEOUT
cls
ECHO Exiting because no choices were made in 15 seconds
:END
timeout /t 5

Solution

  • This is a very simple way to do that:

    @ECHO OFF
    :start
    set option=0
    
    ECHO 1. Clear IE, Chrome, Temp Cache
    ECHO 2. Clear Credentials in IE, Chrome, and Windows
    ECHO 3. Clear All Options
    ECHO 4. Exit
    CHOICE /N /C:12345 /T 15 /D 5 /M "Type the number to choose an option."
    GOTO Option-%ERRORLEVEL%
    
    :Option-3 ClearAllOptions
    ECHO Clearing All Options
    set option=3
    
    :Option-1 ClearCache
    ECHO Clearing Cache...
    <code here>
    pause
    cls
    if %option% neq 3 GOTO :start
    
    REM ===-----------------------
    
    :Option-2 ClearCredentials
    ECHO Clearing Credentials
    <code here>
    pause
    cls
    GOTO :start
    
    REM ===-----------------------
    
    :Option-4 Exit
    ECHO Exiting...
    <code here>
    pause
    EXIT
    
    REM ===-----------------------
    
    :Option-5 TIMEOUT
    cls
    ECHO Exiting because no choices were made in 15 seconds
    :END
    timeout /t 5