9

BAT stop with countdown delay

 3 years ago
source link: https://www.codesd.com/item/bat-stop-with-countdown-delay.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

BAT stop with countdown delay

advertisements

Windows 7 CMD.exe style Batch

I am unsure this will work flawlessly. I feel there will be times when the TIMEOUT finishes and the bat executes the SHUTDOWN -ABORT on it's own. I want the user to simply press one key to stop the shutdown countdown (ideally just the COMMENT "OK" button would be enough), otherwise to flawlessly shutdown the computer.

if exist "C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" ("C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" %1 %2 %3 %4 %5 %6 %7 %8 %9 %10 vlc://quit) else goto end
shutdown -s -t 120 -c "Press any key in the Command Prompt to cancel the shutdown."
timeout -t 120
shutdown -a

I considered adding a PING 999.999.999.999 -n 1 -w 1000 to force a short wait, but if the user presses a key then it would wait before aborting the shutdown and that could also lead to a problem.


This uses Choice to provide the delay and an errorlevel to check, so that it will abort if C is pressed. It also provides an extra option to shutdown immediately.

Timeout doesn't provide an errorlevel on abort so pressing any key to abort doesn't seem to be an option.

@echo off
if exist "C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" ("C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" %1 %2 %3 %4 %5 %6 %7 %8 %9 %10 vlc://quit) else (goto :EOF)
choice /c CS /D S /T 120 /M "Waiting for 120 seconds: Press C to cancel shutdown, or S to shutdown now"
if errorlevel 2 shutdown -s




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK