You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

未选择选项按回车时批处理程序意外关闭的解决方法

解决批处理菜单空回车直接退出的问题

你的问题出在SET /P命令直接回车时,变量M会为空,原脚本没有处理空值的逻辑,导致直接走到GOTO exit关闭窗口。只需在每个菜单的输入判断前添加空值检查,就能让空回车返回当前菜单。另外还修正了winhealth菜单里的选项序号对应错误(原脚本中选项2重复指向两个标签,选项3-7的跳转全部错位)。

以下是修改后的完整脚本:

@echo off
:mainmenu
cls
color 1f
echo.
echo  =========================================================
echo  =        R U N   A S   A D M I N I S T R A T O R        =
echo  =========================================================
echo.
echo.
echo     WINDOWS COMMAND UTILITY (MAIN MENU):
echo     ------------------------------------
echo.
echo     [1] - BOOT COMMANDS
echo.
echo     [2] - WINDOWS HEALTH COMMANDS
echo.
echo     [0] - Exit Menu (Closes this window)
echo.
echo.
SET /P M=Enter option [Number] then press ENTER:
REM 处理空回车,返回主菜单
IF "%M%"=="" GOTO mainmenu
IF %M%==1 GOTO bootcommands
IF %M%==2 GOTO winhealth
IF %M%==0 GOTO exit
REM 输入无效值也返回主菜单,避免直接退出
GOTO mainmenu

:bootcommands
cls
color 4e
echo.
echo  =========================================================
echo  =        R U N   A S   A D M I N I S T R A T O R        =
echo  =========================================================
echo.
echo.
echo     BOOT COMMANDS:
echo     --------------
echo.
echo     [1] - Windows (Normal Startup)
echo.
echo     [2] - Windows Safe Mode (Offline)
echo.
echo     [3] - Windows Safe Mode with Networking
echo.
powershell write-host -fore yellow -back red Tampering with the UEFI/Bios may render the PC unbootable!
echo     [8] - Boot into UEFI/Bios
echo.
echo     [0] - Return to MAIN MENU
echo.
echo.
SET /P M=Enter option [Number] then press ENTER:
REM 处理空回车,返回当前菜单
IF "%M%"=="" GOTO bootcommands
IF %M%==1 GOTO normal
IF %M%==2 GOTO safe
IF %M%==3 GOTO safenetwork
If %M%==8 GOTO bootbios
IF %M%==0 GOTO mainmenu
REM 输入无效值也返回当前菜单
GOTO bootcommands

:normal
bcdedit /deletevalue {default} safeboot
goto reboot

:safe
bcdedit /set {default} safeboot minimal
goto reboot

:safenetwork
bcdedit /set {default} safeboot network
goto reboot

:bootbios
shutdown /r /fw /f /t 2
exit

:reboot
shutdown /f /t 2

:winhealth
cls
color 1f
echo.
echo  =========================================================
echo  =        R U N   A S   A D M I N I S T R A T O R        =
echo  =========================================================
echo.
echo.
echo     WINDOWS HEALTH COMMANDS:
echo     ------------------------
echo.
echo     [1] - *Create a System Restore Point
echo.
echo     [2] - Elevated Command Prompt (Administrator Privileges)
echo.
echo     [3] - SFC (Offline) File System Checker
echo.
echo     [4] - DISM CheckHealth (Offline) Deployment Image Servicing and Management
echo.
echo     [5] - DISM ScanHealth (Offline) Deployment Image Servicing and Management
echo.
powershell write-host -fore red -back yellow Option 6 requires Internet Connection!
echo     [6] - DISM RestoreHealth (Online) Deployment Image Servicing and Management
echo.
echo     [7] - CHKDSK (Offline) Check Disk checks the file system and file system
echo           metadata of a volume for logical and physical errors.
echo.
echo     [0] - Return to MAIN MENU
echo.
echo.
SET /P M=Enter option [Number] then press ENTER:
REM 处理空回车,返回当前菜单
IF "%M%"=="" GOTO winhealth
IF %M%==1 GOTO createrestore
IF %M%==2 GOTO cmdadmin
IF %M%==3 GOTO sfc
IF %M%==4 GOTO dismhealth
IF %M%==5 GOTO dismscan
IF %M%==6 GOTO dismrestore
IF %M%==7 GOTO chkdsk
IF %M%==0 GOTO mainmenu
REM 输入无效值也返回当前菜单
GOTO winhealth

:createrestore
cls
echo.
echo Creating a System Restore point. Please be patient...
echo.
echo.
wmic.exe /Namespace:\\root\default Path SystemRestore Call CreateRestorePoint "Windows Command Utility", 100, 7
echo.
pause
GOTO winhealth

:cmdadmin
start cmd /k
GOTO winhealth

:sfc
cls
echo.
echo Starting System File Checker. This is a utility in Microsoft Windows
echo that scans for and restores corrupted Windows system files. 
cmd /k sfc /scannow
GOTO winhealth

:dismhealth
cls
echo.
echo This is a quick scan and will determine if the image is repairable.
cmd /k DISM /Online /Cleanup-Image /CheckHealth
GOTO winhealth

:dismscan
cls
echo.
echo This is a quick scan and will determine if there are any corruptions detected.
cmd /k DISM /Online /Cleanup-Image /ScanHealth
GOTO winhealth

:dismrestore
cls
echo.
echo Your system will automatically connect to the Windows Update
echo service to download and replace the corrupt files.
cmd /k DISM /Online /Cleanup-Image /RestoreHealth
GOTO winhealth

:chkdsk
cmd /k chkdsk.exe /f /r /x
GOTO winhealth

:exit
exit

修改说明:

  1. 空回车处理:在每个SET /P后添加IF "%M%"=="" GOTO 当前菜单标签,直接回车时返回当前菜单,不会退出。
  2. 无效输入处理:把原来的GOTO exit改成返回当前菜单,输入错误值时也不会直接关闭窗口。
  3. 修正菜单序号错误:修复winhealth菜单里选项3-7的跳转错位问题,确保每个选项对应正确功能。
  4. 优化返回逻辑:把各功能标签后的exit改成返回对应子菜单,执行完操作后不会直接关闭程序,方便继续使用。

内容的提问来源于stack exchange,提问作者user21229186

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 02:39:25