Home > other >  Batch choice command runs both choices instead of just the one I selected
Batch choice command runs both choices instead of just the one I selected

Time:09-17

I am really confused with this problem. I'm trying to write a power plan switcher (because my laptop seems to have a issue with it constantly resetting to power saving for no reason) and I've got the choice prompt to work.

Both pieces of code work but if I were to type 1 to select no power plan lock it would run the code just fine but then the second choice also runs without asking. I don't really understand why it's happening.

I thought I muddled up the numbers so I double checked them but they are fine.

Here is the code.

 echo power plan locker
 echo by hummingrofl560
 echo please mark your selection below.
 echo ==================================
 echo 1. no plan locked - use this before changing plans. also allows changing of plan settings.
 echo 2. power saving - Saves energy by reducing your computer's performance where possible.
 echo 3. balanced/automatic (default) - automatically balances performance with energy consumption on capable hardware.
 echo 4. high performance - Favours performance but may use more energy.
 echo 5. quiet mode - reduces performance but makes it quieter.
 choice /N /C:12345 /M "please make your choice"%1
IF ERRORLEVEL ==1 GOTO ONE
IF ERRORLEVEL ==2 GOTO TWO
IF ERRORLEVEL ==3 GOTO THREE
IF ERRORLEVEL ==4 GOTO FOUR
IF ERRORLEVEL ==5 GOTO FIVE
:ONE
echo disabling power plan lock.
c:\powerplans\Default_no_specified_power_plan.reg
echo power plan lock disabled.
echo you must restart your computer before new power plan settings can take effect.
pause


:TWO
echo enabling lock for "balanced/automatic"
c:\powerplans\Specify_Automatic_power_plan.reg
echo plan locked to "balanced/automatic".
echo you must restart your computer before new power plan settings can take effect.
pause

CodePudding user response:

There are at least two problems in your code.

You're using the wrong syntax to check the errorlevel.

IF [NOT] ERRORLEVEL number command

ERRORLEVEL number Specifies a true condition if the last program run
returned an exit code equal to or greater than the number specified.

If you try to compare with an exact match use if %ERRORLEVEL% == 1 instead.

Second problem: A function in batch has to end or return somehow.

:one
echo This is one and here it ends
exit /b

:two
echo This is two and here it ends
goto :eof

CodePudding user response:

It turns out that batch does choices a bit oddly where your choices need to be in a descending order (3,2,1) instead of ascending (1,2,3). A quick swap and the problem is fixed.

  • Related