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

批处理脚本功能异常求助:首条ECHO失效、加载屏未实现

批处理脚本修复方案

问题说明

我想制作一个批处理脚本,自动执行原本需手动逐个输入的系统检测命令,实现带检查点的无缝流程。目前脚本仅95%可用:首条ECHO语句被忽略,伪加载屏未集成,且存在流程跳步异常问题。

原脚本及加载屏代码

初始版本脚本

@ECHO OFF
TITLE YEY-Computer-Checker

START /b /wait cmd.exe /c chkdsk /f/r /y

ECHO The program will continue dont worry! This will only be showed once!
timeout /t 5 nobreak
cls

START /b /wait cmd.exe /c sfc /scannow
timeout /t 5 /nobreak
cls

START /b cmd.exe /c DISM /Online /Cleanup-Image /CheckHealth
timeout /t 5 /nobreak
cls

START /b /wait cmd.exe /c DISM /Online /Cleanup-Image /ScanHealth
timeout /t 5 /nobreak
cls

IF NOT ERRORLEVEL 1 START /b /wait cmd.exe /c DISM /Online /Cleanup-Image /RestoreHealth
timeout /t 5 /nobreak
cls

:end ECHO The YEY-Computer-Checker has finished! Your System seems to be alright! The Programm will close in 5s or press Enter to close immediately.

:Error ECHO The Programm has encountered an Error! And will close automatically in 5s.
timeout /t 5

EXIT

1.1版本脚本(95%可用)

@ECHO OFF

TITLE YEY-Computer-Checker

chkdsk /f/r /y

ECHO The program will continue dont worry! This will only be showed once!

timeout /t 10 nobreak >nul

cls

sfc /scannow

timeout /t 10 /nobreak >nul

cls

DISM /Online /Cleanup-Image /CheckHealth

timeout /t 10 /nobreak >nul

cls

DISM /Online /Cleanup-Image /ScanHealth

cls

IF NOT ERRORLEVEL 0 DISM /Online /Cleanup-Image /RestoreHealth

timeout /t 10 /nobreak >nul

cls

ECHO Do you wish to do a [Q]uick Scan a [F]ull Scan or [N]either of the two?
ECHO\
ECHO A Quick Scan will take approx. 10 mins.
ECHO\
ECHO A Full Scan will take longer, approx. 60 mins.

:again

set /p choice=

IF %choice%==Q (

    "C:\Program Files\Windows Defender\MpCmdRun.exe" -Scan -ScanType 1

)


IF %choice%==F (

    "C:\Program Files\Windows Defender\MpCmdRun.exe" -Scan -ScanType 2
)


IF %choice%==N ECHO Ok, remember to do a scan manually just in case!

IF NOT %choice%==Q IF NOT %choice%==F IF NOT %choice%==N ECHO Please type the exact letter as shown (without the square brackets) and press Enter! & GOTO again

timeout /t 10 >nul

cls

ECHO The YEY-Computer-Checker has finished! Your System seems to be alright!
ECHO\
ECHO The Programm will automatically close in 10s or press Enter to close immediately.

timeout /t 10

EXIT

伪加载屏代码

@ECHO OFF

SET program=MpCmdRun.exe

TASKLIST |FIND /i "%program%" >nul

:loading

IF %ERRORLEVEL% == 0 (
    
    ECHO Windows Defender is currently scanning!
    ECHO [============]
    TIMEOUT /t 1 >nul
    cls
    ECHO Windows Defender is currently scanning!
    ECHO [========================]
    TIMEOUT /t 1 >nul
    cls
    ECHO Windows Defender is currently scanning!
    ECHO [====================================]
    TIMEOUT /t 1 >nul
    cls
    ECHO Windows Defender is currently scanning!
    ECHO [================================================]
    TIMEOUT /t 1 >nul
    cls
    ECHO Windows Defender is currently scanning!
    ECHO [============================================================]
    TIMEOUT /t 1 >nul
    cls
    IF NOT %ERRORLEVEL% == 0 EXIT
    GOTO loading
)

修复后的完整脚本

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
TITLE YEY-Computer-Checker

:: 执行CHKDSK,处理磁盘检查后的输出问题
chkdsk /f /r /y >nul 2>&1
cls
ECHO 程序将继续执行,请不要担心!此提示仅显示一次!
timeout /t 10 /nobreak
cls

:: 执行SFC扫描,等待完成后继续
sfc /scannow
timeout /t 10 /nobreak >nul
cls

:: 执行DISM检查健康状态
DISM /Online /Cleanup-Image /CheckHealth
timeout /t 10 /nobreak >nul
cls

:: 执行DISM扫描健康状态,根据结果决定是否修复
DISM /Online /Cleanup-Image /ScanHealth
IF ERRORLEVEL 1 (
    DISM /Online /Cleanup-Image /RestoreHealth
)
timeout /t 10 /nobreak >nul
cls

:: 选择扫描类型
ECHO 请选择扫描类型:[Q]快速扫描  [F]全面扫描  [N]都不执行
ECHO.
ECHO 快速扫描约需10分钟
ECHO.
ECHO 全面扫描约需60分钟

:again
set "choice="
set /p choice=请输入选择:
IF NOT DEFINED choice (
    ECHO 请输入有效选项!
    GOTO again
)
:: 统一转为大写,兼容小写输入
set "choice=!choice:~0,1!"
set "choice=!choice:q=Q!"
set "choice=!choice:f=F!"
set "choice=!choice:n=N!"

IF "!choice!"=="Q" (
    :: 启动加载屏后台进程
    START /MIN "LoadingScreen" "%~dp0loading.bat"
    "C:\Program Files\Windows Defender\MpCmdRun.exe" -Scan -ScanType 1
    :: 终止加载屏进程
    TASKKILL /FI "WINDOWTITLE eq LoadingScreen" /F >nul 2>&1
    cls
) ELSE IF "!choice!"=="F" (
    START /MIN "LoadingScreen" "%~dp0loading.bat"
    "C:\Program Files\Windows Defender\MpCmdRun.exe" -Scan -ScanType 2
    TASKKILL /FI "WINDOWTITLE eq LoadingScreen" /F >nul 2>&1
    cls
) ELSE IF "!choice!"=="N" (
    ECHO 好的,记得手动执行扫描!
    timeout /t 5 >nul
    cls
) ELSE (
    ECHO 请输入正确的字母(无需括号),然后按回车!
    GOTO again
)

:: 完成提示
ECHO YEY-Computer-Checker 已完成!系统状态正常!
ECHO.
ECHO 程序将在10秒后自动关闭,或按回车立即关闭。
timeout /t 10

EXIT

伪加载屏脚本(保存为loading.bat,放在同目录下)

@ECHO OFF
TITLE LoadingScreen

SET program=MpCmdRun.exe

:loading
:: 每次循环重新检查进程是否存在
TASKLIST | FIND /i "%program%" >nul
IF ERRORLEVEL 1 (
    EXIT
)

ECHO Windows Defender 正在扫描!
ECHO [============]
TIMEOUT /t 1 >nul
cls
ECHO Windows Defender 正在扫描!
ECHO [========================]
TIMEOUT /t 1 >nul
cls
ECHO Windows Defender 正在扫描!
ECHO [====================================]
TIMEOUT /t 1 >nul
cls
ECHO Windows Defender 正在扫描!
ECHO [================================================]
TIMEOUT /t 1 >nul
cls
ECHO Windows Defender 正在扫描!
ECHO [============================================================]
TIMEOUT /t 1 >nul
cls

GOTO loading

关键修复点说明

  1. 首条ECHO被忽略问题:

    • 在chkdsk执行后添加cls清屏,避免chkdsk的输出覆盖后续ECHO内容
    • 将chkdsk的输出重定向到nul,减少无关输出干扰
  2. 流程跳步异常问题:

    • 修正DISM修复的判断逻辑:IF ERRORLEVEL 1表示扫描失败时执行修复,符合ERRORLEVEL的判断规则(ERRORLEVEL n 代表退出码≥n)
    • 给set /p choice添加空输入判断,避免空值导致脚本报错跳步
    • 启用SETLOCAL ENABLEDELAYEDEXPANSION处理变量的动态赋值,避免变量替换异常
    • 统一输入选项的大小写,兼容用户输入小写的情况
  3. 伪加载屏集成:

    • 将加载屏保存为独立的loading.bat,在启动扫描时用START /MIN后台运行
    • 扫描完成后,通过窗口标题终止加载屏进程,实现扫描过程中动态加载动画的效果

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 03:14:59