批处理脚本异常:错误输入分支无法执行倒计时关机逻辑
批处理错误密码分支故障修复
正确输入密码时,脚本的验证通过分支能正常执行;但输入错误密码时,验证失败分支无法按预期运行——窗口会直接关闭,无法完成从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 )
问题根源
- 标签不能放在括号代码块内:
:LOOP和:END标签定义在if /I NOT的括号代码块里,批处理解析时会直接跳出代码块,导致后续的shutdown命令根本不会执行,窗口直接关闭。 - 变量延迟扩展问题:循环中修改
SecondsLeft变量后,直接用%SecondsLeft%读取会获取初始值,无法正确显示倒计时。 - 倒计时逻辑错误:初始值设为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
修改说明
- 启用延迟扩展:添加
setlocal enabledelayedexpansion,循环中用!SecondsLeft!读取变量,确保每次都能获取最新值。 - 移除外置标签:将
:LOOP标签移到所有括号代码块外部,避免破坏代码结构。 - 调整倒计时逻辑:初始值设为5,循环先显示当前秒数,再减1,直到-1时退出循环,确保从5倒计时到0都能显示。
- 简化代码:移除多余的
call echo和无效的:END标签,call :LOOP不需要传参,直接使用全局变量。 - 添加退出逻辑:正确分支执行完后用
exit /b退出,避免继续执行错误分支代码。
内容的提问来源于stack exchange,提问作者Batch_Compiler15
相关产品推荐
相关产品推荐

