How to request user input for a batch file with a timeout

So, I'm trying to add a timer to one of the if statements from the set command, but I'm not sure what the command is. the script will start and thirty minutes before restarting the PC, either wait for the user to enter input at this time or cancel it. Therefore, I have two if statements for "reboot now" and "cancel", but now I need the if statement to return an account thirty minutes before it executes my reboot command. Also, if anyone knows how to add a visual timer there, showing how much time is left, which 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 
+4
source share
5 answers

Similar for sleep mode.

 @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 
+6
source

I recommend using CHOICE.EXE , it comes with most versions of Windows (with the exception of Windows NT, 2000 and XP, it was used to download from the Microsoft website, but they seem to have missed this * one on their ftp site.) And easy 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 /dn /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 

Just CHOICE.EXE works like this ...

 choice 

... and coincides with ...

 choice /c yn 

... both will be displayed ...

 [Y,N]? 

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

The selection saves the result in% errorlevel%. Y = 1, N = 2.

My code uses the default /D <choice> and timeout /T <seconds> options.

In the example ...

 choice /c yn /dy /t 5 

... gives the user a choice of Y or N, will wait 5 seconds, then automatically selects the default selection of Y, resulting in% ERRORLEVEL% == 1.

Another example:

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

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

No ERRORLEVEL 0.

For details on using choice enter CHOICE /? on the command line.


* NOTE CHOICE.EXE version I have provided a link for using slightly different commands, but it provides the same functionality.

+5
source

Here is a very simple solution for Vista and Windows 7 that provides a timeout function but does not provide 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's a more sophisticated solution for Vista and Windows 7 that provides a visual countdown but clears the console window each time. Also the time 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, I think you will need to download a non-native command line tool that asks for input with a timeout function, or switch to VBScript or JScript.

EDIT

Both of the scenarios above can be adapted to run on XP by downloading CHOICE.EXE from the Microsoft FTP site , which James K provided in his answer.

This version of CHOICE has a 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 is the coolest XP compatible VBS solution

 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, and I really know little about this technology.

+1
source

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

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

This will send 1 ping to the IP address 1.1.1.1 after the expiration of <milliseconds> . And the output of the ping command will be rejected to NUL.

0
source

I recommend this script by SPC_75 its .vbs file, although for sleep mode, but it is easy to modify it 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 great with warning and timeout.

0
source

Source: https://habr.com/ru/post/1433137/


All Articles