Question

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.

Was it helpful?

Solution

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

OTHER TIPS

Look into using the choice command. You may need to download it, depending on your OS.

This will wait 300 seconds before shutting down, if q is not pressed.

choice /c aq /n /d a /t 300 /m "Computer shutting down in 300 seconds [q to cancel]"
if %errorLevel%==1 shutdown -a
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top