首页 > 解决方案 > 批处理选择命令运行两个选项,而不仅仅是我选择的那个

问题描述

我真的对这个问题感到困惑。我正在尝试编写一个电源计划切换器(因为我的笔记本电脑似乎有问题,它会无缘无故地不断重置为省电模式)并且我有选择提示可以工作。

两段代码都可以工作,但如果我输入 1 来选择没有电源计划锁定,它会运行代码就好了,但是第二个选择也运行而无需询问。我真的不明白为什么会这样。

我以为我把数字弄混了,所以我仔细检查了它们,但它们很好。

这是代码。

 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

标签: windowsbatch-filecmdchoiceerrorlevel

解决方案


您的代码中至少存在两个问题。

您使用错误的语法来检查错误级别。

IF [NOT] ERRORLEVEL number command

ERRORLEVEL number 如果最后一个程序运行
返回的退出代码等于或大于指定的数字,则指定一个真条件。

如果您尝试与完全匹配进行比较,请if %ERRORLEVEL% == 1改用。

第二个问题:批处理中的函数必须以某种方式结束或返回。

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

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

推荐阅读