How to ask for batch file user input with a timeou

2019-01-26 13:52发布

问题:

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

回答1:

Similar one for hibernate.

@echo off
setlocal enableDelayedExpansion
for /l %%N in (600 -1 1) do (
  set /a "min=%%N/60, sec=%%N%%60, n-=1"
  if !sec! lss 10 set sec=0!sec!
  cls
  choice /c:CN1 /n /m "HIBERNATE in !min!:!sec! - Press N to hibernate Now, or C to Cancel. " /t:1 /d:1
  if not errorlevel 3 goto :break
)
cls
echo HIBERNATE in 0:00 - Press N to hibernate Now, or C to Cancel.
:break
if errorlevel 2 (%windir%\System32\rundll32.exe powrprof.dll,SetSuspendState Hibernate) else echo Hibernate Canceled


回答2:

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.

@Echo off
:START
set waitMins=30

echo PC WILL RESTART IN %waitMins% MINUTES: Press N to restart [N]ow or C to [C]ancel
:: Calculate Seconds
set /a waitMins=waitMins*60
:: Choices are n and c, default choice is n, timeout = 1800 seconds
choice /c nc /d n /t %waitMins%

:: [N]ow = 1; [C]ancel = 2
goto Label%errorlevel%

:Label1

shutdown -r -t 60 -f
:: Make sure that the process doesn't fall through to Lable2
goto :eof

:Label2 
exit

Simply CHOICE.EXE works like this...

choice

...and is the same as...

choice /c yn

...both will display...

[Y,N]?

...and both will wait for the user to press a Y or N.

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...

choice /c yn /d y /t 5

...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...

choice /c abcdef /m "Make a choice. "

...and it displays...

Make a choice. [A,B,C,D,E,F]? 

...and...

A = %ERRORLEVEL% = 1
B = %ERRORLEVEL% = 2
C = %ERRORLEVEL% = 3
D = %ERRORLEVEL% = 4
E = %ERRORLEVEL% = 5
F = %ERRORLEVEL% = 6

There is no ERRORLEVEL 0.

For more on the use of choice, type CHOICE /? at the command prompt.


*NOTE The version of CHOICE.EXE I provided a link to uses slightly different commands, but provides the same functionality.



回答3:

Here is a really simple solution for Vista and Windows 7 that provides the timeout feature, but does not give a visual countdown.

@echo off
choice /c:CN /n /m "PC will restart in 30 minutes. Press N to restart Now, or C to Cancel" /t:1800 /d:N
if errorlevel 2 (shutdown -r -t 60 -f) else echo Restart Canceled

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.

@echo off
setlocal enableDelayedExpansion
for /l %%N in (1800 -1 1) do (
  set /a "min=%%N/60, sec=%%N%%60, n-=1"
  if !sec! lss 10 set sec=0!sec!
  cls
  choice /c:CN1 /n /m "PC will restart in !min!:!sec! - Press N to restart Now, or C to Cancel. " /t:1 /d:1
  if not errorlevel 3 goto :break
)
cls
echo PC will restart in 0:00 - Press N to restart Now, or C to Cancel.
:break
if errorlevel 2 (shutdown -r -t 60 -f) else echo Restart Canceled

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:

choice /c:CN /n /t:N,1800 "PC will restart in 30 minutes. Press N to restart Now, or C to Cancel"

To adapt my second script, use:

choice /c:CN1 /n /t:1,1 "PC will restart in !min!:!sec! - Press N to restart Now, or C to Cancel. "


EDIT - Here is a crude VBS solution that is compatible with XP

Set objShell = CreateObject("WScript.Shell")
for i = 30 to 1 step -1
  if i=1 then unit=" minute" else unit=" minutes"
  rtn = objShell.Popup ( _
    "The machine would like to Restart."&VbCrLf&VbCrLf& _
    "Click OK to restart now"&VbCrLf& _
    "Click Cancel or the [X] close button to abort the restart"&VbCrLf& _
    "Do nothing and the machine will restart in "&i&unit, _
    60, "Restart in "&i&unit, 1+48 _
  )
  if rtn <> -1 then exit for
next
if rtn <> 2 then objShell.Run "shutdown -r -f"

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.



回答4:

I would use this code to make the script pause for a specified number of milliseconds:

PING 1.1.1.1 -n 1 -w <milliseconds> >NUL 

This will send out 1 ping to the IP address 1.1.1.1 after <milliseconds> has elapsed. And the output of the ping command will be dismissed to NUL.



回答5:

I recommend this script by SPC_75 its a .vbs file though, and for hibernate, but its easy to modify for sleep or shutdown.

'//*******************************************
'//hibernate.vbs
'//Purpose: Count Down to action (Hibernate)
'//Tested on Server 2008, Win 7 64bit, and XP
'//Author: SPC_75 
'//Revision 1.3
'//Date: 1/09/2011
'//*******************************************

Option Explicit
Dim timeout, objShell, intReturn
Const wshOk = 1
Const wshOkDialog = 0
'Const wshIcon = 16 '/critical
'Const wshIcon = 32 '/question
Const wshIcon = 48 '/exclamation
'Const wshIcon = 64 '/information

timeout = 30 '/Timeout in seconds

Set objShell = CreateObject("Wscript.Shell")
  Do Until timeout = 0
    timeout = timeout - 1
    intReturn = objShell.Popup(vbCrlf &"Hibernation about to initiate. Abort?"&vbCrlf & vbCrlf & vbCrlf & vbCrlf & "Time until hibernation : " & timeout, 1, "Hibernation", wshOkDialog + wshIcon + 4096)
    If intReturn = wshOk Then
        Wscript.Quit
    End If
  loop
  objShell.Run "powercfg /hibernate on"
  objShell.Run "shutdown -h"
  objShell.Run "rundll32 powrprof.dll,SetSuspendState Hibernate"  '/XP specific Hibernation command
set objShell = nothing

'//EOF

Works perfectly with a warning and timeout.