0

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
dan1st
  • 12,568
  • 8
  • 34
  • 67
  • Take a look at this: [Batch file: Two consecutive IFs](https://stackoverflow.com/a/68543566). Whilst the question is different, the issue is caused by exactly the same mistake… – aschipfl Aug 31 '21 at 14:50

2 Answers2

4

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
jeb
  • 78,592
  • 17
  • 171
  • 225
-1

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.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • 5
    hummingrofl860, the help information for the built in choice.exe utility is readily available from `[ENTER]`ing `choice /?`, in a Command Prompt window. That output specifically states, "When you use ERRORLEVEL parameters in a batch program, list them in decreasing order." Therefore your reported issue was simply that you had failed to read the usage information for the command you were using. For that reason your solution was simply to read that information, and as everyone should do that before seeking solutions, to a problem with one or more commands, it is of no future use to our readers. – Compo Aug 31 '21 at 14:06
  • 1
    Glad to hear your problem is resolved. For this to be an answer it needs more details. Why does choice order matter? – solbs Aug 31 '21 at 16:18
  • 1
    Please add further details to expand on your answer, such as working code or documentation citations. – Community Aug 31 '21 at 16:19
  • Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Aug 31 '21 at 19:51