Timeout for canceling batch file when entering password

I don't know how to cancel the timeout when the password typed it. Also it doesn't show until I find that at this point the computer will exit due to an event

@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

      

+3


source to share


1 answer


It was a little more difficult than originally. I have commented on everything, but let me know if you have any questions.



@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 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
)

      

+3


source







All Articles