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

批处理脚本异常:错误输入分支无法执行倒计时关机逻辑

批处理错误密码分支故障修复

正确输入密码时,脚本的验证通过分支能正常执行;但输入错误密码时,验证失败分支无法按预期运行——窗口会直接关闭,无法完成从5开始的倒计时并触发关机。问题区域已用Rem标记。

原脚本代码:

@echo off
mode con: cols=1366 lines=768
TITLE Secur_AI winx64
echo This PC is password protected.
set comp=
set /p comp=Enter Password:

if /I "%comp%"=="Coachify" (
    echo Checking Password...
    timeout /t 03 /nobreak >NUL
    @echo off
    color 02
    echo Password: Correct
    ping 127.0.0.1 -n 1 -w 500 >nul
    echo Establishing Firewall....
    timeout /t 03 /nobreak >NUL
    echo Firewall: Established
    ping 127.0.0.1 -n 1 -w 500 >nul
    echo Establishing Secure Server Connection.....
    timeout /t 03 /nobreak >NUL
    echo Secure Connection: Established
    ping 127.0.0.1 -n 1 -w 500 >nul
    msg * Welcome!
    timeout /t 02 /nobreak >NUL
)

if /I NOT "%comp%"=="Coachify" (
    echo Checking Password...
    timeout /t 03 /nobreak >NUL
    @echo off
    color 04
    TITLE Self_Shutdown_Prompt
    echo Wrong Password.
    ping 127.0.0.1 -n 1 -w 100 >nul
    echo Intruder Detected.
    ping 127.0.0.1 -n 1 -w 100 >nul
    echo Initiating Protection Mode...
    timeout /t 03 /nobreak >NUL
    echo Protection Mode Initiated.
    timeout /t 01 /nobreak >NUL
    set SecondsLeft=6
    call :LOOP %SecondsLeft%
    goto END

    :LOOP
    cls
    set /a SecondsLeft=%SecondsLeft%-1

    Rem Start of problem area.
    if %SecondsLeft% == 0 goto :eof
    call echo Initiating SelfShutdown in... %SecondsLeft% seconds.
    ping -n 2 127.0.0.1 >nul
    goto LOOP

    :END
    Rem End of problem area.

    shutdown -s -t 00
)

问题根源

  1. 标签不能放在括号代码块内::LOOP和:END标签定义在if /I NOT的括号代码块里,批处理解析时会直接跳出代码块,导致后续的shutdown命令根本不会执行,窗口直接关闭。
  2. 变量延迟扩展问题:循环中修改SecondsLeft变量后,直接用%SecondsLeft%读取会获取初始值,无法正确显示倒计时。
  3. 倒计时逻辑错误:初始值设为6,第一次减1后是5,但结束判断goto :eof会直接退出call调用,不会触发关机。

修正后的脚本

@echo off
setlocal enabledelayedexpansion
mode con: cols=1366 lines=768
TITLE Secur_AI winx64
echo This PC is password protected.
set comp=
set /p comp=Enter Password:

if /I "%comp%"=="Coachify" (
    echo Checking Password...
    timeout /t 03 /nobreak >NUL
    color 02
    echo Password: Correct
    ping 127.0.0.1 -n 1 -w 500 >nul
    echo Establishing Firewall....
    timeout /t 03 /nobreak >NUL
    echo Firewall: Established
    ping 127.0.0.1 -n 1 -w 500 >nul
    echo Establishing Secure Server Connection.....
    timeout /t 03 /nobreak >NUL
    echo Secure Connection: Established
    ping 127.0.0.1 -n 1 -w 500 >nul
    msg * Welcome!
    timeout /t 02 /nobreak >NUL
    endlocal
    exit /b
)

if /I NOT "%comp%"=="Coachify" (
    echo Checking Password...
    timeout /t 03 /nobreak >NUL
    color 04
    TITLE Self_Shutdown_Prompt
    echo Wrong Password.
    ping 127.0.0.1 -n 1 -w 100 >nul
    echo Intruder Detected.
    ping 127.0.0.1 -n 1 -w 100 >nul
    echo Initiating Protection Mode...
    timeout /t 03 /nobreak >NUL
    echo Protection Mode Initiated.
    timeout /t 01 /nobreak >NUL
    set SecondsLeft=5
    call :LOOP
    shutdown -s -t 00
    endlocal
    exit /b
)

:: 把标签移到代码块外部
:LOOP
cls
echo Initiating SelfShutdown in... !SecondsLeft! seconds.
ping -n 2 127.0.0.1 >nul
set /a SecondsLeft-=1
if !SecondsLeft! geq 0 goto LOOP
goto :eof

修改说明

  1. 启用延迟扩展:添加setlocal enabledelayedexpansion,循环中用!SecondsLeft!读取变量,确保每次都能获取最新值。
  2. 移除外置标签:将:LOOP标签移到所有括号代码块外部,避免破坏代码结构。
  3. 调整倒计时逻辑:初始值设为5,循环先显示当前秒数,再减1,直到-1时退出循环,确保从5倒计时到0都能显示。
  4. 简化代码:移除多余的call echo和无效的:END标签,call :LOOP不需要传参,直接使用全局变量。
  5. 添加退出逻辑:正确分支执行完后用exit /b退出,避免继续执行错误分支代码。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 06:14:56