键入密码时,我不知道如何取消超时。也不会显示,直到我按Enter键后,由于该事件计算机注销

@echo off

:TIMER
pause
timeout 60

shutdown -l

:LOGIN
echo Enter password to stop the timer!
set /p pass ="Password: "

if NOT %pass%== Password goto :FAIL
if %pass%== Password goto :Success

:FAIL
echo Invalid password please try again!
goto :LOGIN

:SUCCESS
exit

最佳答案

这比最初出现的要困难一些。我已经评论了所有内容,但是如果您有任何疑问,请告诉我。

@echo off

pause

REM We need to start the timer and shutdown in a separate window because batch
REM can't do multiple things at once. Also, we're starting the second window
REM minimized mostly for aesthetics, but also to reduce the chance of the user
REM pressing a key by accident and killing the timer without diffusing the stop.
REM
REM The /t 10 waits 10 seconds before shutting the computer down. Unfortunately,
REM the /l option to log out does not allow the /t switch to be used. This adds
REM a bigger element of danger, anyway. The way the code works, you'll still get
REM a pop-up that says that Windows is shutting down in less than a minute, but
REM the shutdown will be aborted.
start "" /min cmd /c "timeout 60&shutdown /s /t 10"

:LOGIN
echo Enter the password to stop the timer!

REM I moved the left quote to the left of pass so that quotes would still be in
REM effect but they wouldn't be visible. I also removed the space between pass
REM and the = because the way it was before, you had a variable that would have
REM to be accessed as %pass % because you can put spaces in variable names for
REM some reason.
set /p "pass=Password: "

REM I've put quotes around both %pass% and Password to prevent a syntax error
REM that would occur if the user didn't enter anything. Also, I got rid of the
REM space to the left of Password because the way it was originally written,
REM the password would have been <space>Password and I wasn't sure if that was
REM deliberate. If it was, just add it back in.
if not "%pass%"=="Password" (
    echo Incorrect password! Please try again!
    goto :LOGIN
) else (
    echo CORRECT!

    REM Thankfully, the timeout command is a separate executable from the rest
    REM of CMD, so we can just simply kill it. Note that if you have any other
    REM timeouts running, this will kill them as well. The >nul is to prevent
    REM a "The process timeout.exe with PID 12345 has been terminated" message.
    taskkill /F /IM timeout.exe >nul

    REM I've added a one-second(-ish) pause between aborting the timeout and
    REM aborting the shutdown so that the shutdown command actually has a
    REM chance to start. Otherwise, there's a race condition where the abort
    REM might run before the shutdown is triggered and that would be really bad.
    ping 127.0.0.1 -n 2 >nul

    REM This just aborts the shutdown command.
    shutdown /a
)

10-07 23:57