So I am trying to add a timer to one of my if statements under a set command but I'm not sure what the command would be. The script will launch and wait thirty minutes before it reboots the PC or wait for a users input to input it at that time or cancel it. So I have my two if statements for the "restart now" and "cancel" set but now I need an if statement to have it count down from thirty minutes before it executes my restart command. Also if anyone knows how to add a visual timer on there showing how much time is left that would be a plus. Thanks guys!!!
@Echo off
:START
set /p answer=PC WILL RESTART IN 30 MINUTES, PRESS N TO RESTART NOW OR C TO CANCEL
if "%answer%"=="n" (GOTO Label1)
if "%answer%"=="c" (GOTO Label2)
if "TIMER GOES HERE" "==" (GOTO Label1)
:Label1
shutdown -r -t 60 -f
:Label2
exit
I would use this code to make the script pause for a specified number of milliseconds:
This will send out 1 ping to the IP address
1.1.1.1
after<milliseconds>
has elapsed. And the output of theping
command will be dismissed to NUL.Here is a really simple solution for Vista and Windows 7 that provides the timeout feature, but does not give a visual countdown.
Here is a more complex solution for Vista and Windows 7 that provides a visual countdown, but it clears the console window each second. Also the timing is probably a bit off.
If you need an XP solution then I think you will either need to download a non-native command line tool that asks for input with a timeout feature, or else switch to VBScript or JScript.
EDIT
Both scripts above can be adapted to run on XP by using the CHOICE.EXE download from the Microsoft FTP site that James K provided in his answer.
That version of CHOICE has slightly different syntax.
To adapt my first script, use:
To adapt my second script, use:
EDIT - Here is a crude VBS solution that is compatible with XP
I think you can provide a more elegant VBS solution using HTA, but that is a lot more work, and I don't really know much about that technology.
I reccomend using
CHOICE.EXE
, it comes standard with most versions of Windows (with the exception of Windows NT, 2000 and XP, it used to be downloadable from Microsoft's website, but they seem to have overlooked this* one on their ftp site.) and is simple to use.Simply
CHOICE.EXE
works like this......and is the same as...
...both will display...
...and both will wait for the user to press a
Y
orN
.Choice stores the result in %errorlevel%. Y=1, N=2.
The code I provided takes advantage of the default
/D <choice>
and timeout/T <seconds>
options.In example...
...gives the user a choice of Y or N, will wait for 5 seconds then automaticlly select the default choice of Y, resulting in %ERRORLEVEL%==1.
Another example is...
...and it displays...
...and...
There is no ERRORLEVEL 0.
For more on the use of
choice
, typeCHOICE /?
at the command prompt.*NOTE The version of
CHOICE.EXE
I provided a link to uses slightly different commands, but provides the same functionality.Similar one for hibernate.
I recommend this script by SPC_75 its a .vbs file though, and for hibernate, but its easy to modify for sleep or shutdown.
Works perfectly with a warning and timeout.