windows - 批处理选择命令运行两个选项,而不仅仅是我选择的那个
问题描述
我真的对这个问题感到困惑。我正在尝试编写一个电源计划切换器(因为我的笔记本电脑似乎有问题,它会无缘无故地不断重置为省电模式)并且我有选择提示可以工作。
两段代码都可以工作,但如果我输入 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
解决方案
您的代码中至少存在两个问题。
您使用错误的语法来检查错误级别。
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
推荐阅读
- python - TypeError:使用 seaborn.countplot() 时,参数 'obj' 的类型不正确(预期列表,得到 DataFrame)
- python - 如何积分耦合微分方程?
- angular - 如何在看跌期权角度压缩身体?
- vue.js - Handsontable 中更新内容数据的问题
- node.js - useNewUrlParser:当我们在密码 mongodb nodejs driver 3.1.8 中有 @ 时的真正问题
- javascript - 获取地图框地图范围内的标记列表
- javascript - WordPress 上每个页面的自定义 CSS 和 JS
- php - 如何从此查询中仅获取名称?
- go - 限制从服务器下载
- scala - SKEW 连接提示的 Spark Scala 等效项