如何通过超时请求批处理文件用户输入

时间:2012-09-08 14:04:27

标签: if-statement batch-file timer shutdown restart

所以我试图在set命令下为我的一个if语句添加一个计时器,但我不确定该命令是什么。该脚本将启动并等待30分钟,然后重新启动PC或等待用户输入此时输入或取消它。所以我有两个if语句用于“立即重启”和“取消”设置但现在我需要一个if语句让它从执行我的重启命令之前的三十分钟倒计时。此外,如果有人知道如何在那里添加一个视觉计时器显示剩余多少时间将是一个加号。谢谢你们!

@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

5 个答案:

答案 0 :(得分:7)

我推荐使用CHOICE.EXE,它是大多数Windows版本的标准配置(Windows NT,2000和XP除外,以前可以从微软的网站上下载,但它们似乎忽略了{{3}在他们的ftp网站上有一个。)并且使用起来很简单。

@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

简单CHOICE.EXE就像这样......

choice

......和......一样......

choice /c yn

......两者都会显示......

[Y,N]?

...两者都会等待用户按YN

Choice将结果存储在%errorlevel%中。 Y = 1,N = 2。

我提供的代码利用了默认的/D <choice>和超时/T <seconds>选项。

在示例......

choice /c yn /d y /t 5

...给用户选择Y或N,等待5秒然后自动选择Y的默认选项,结果%ERRORLEVEL%== 1.

另一个例子是......

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

......它显示......

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

...和...

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

没有ERRORLEVEL 0。

有关使用choice的更多信息,请在命令提示符下键入CHOICE /?


* 注意 CHOICE.EXE的版本我提供了一个使用略有不同命令的链接,但提供了相同的功能。

答案 1 :(得分:7)

类似于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 :(得分:2)

这是一个非常简单的Vista和Windows 7解决方案,它提供了超时功能,但没有提供可视倒计时。

@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

这是一个更复杂的Vista和Windows 7解决方案,提供可视倒计时,但它每秒都会清除控制台窗口。时间也可能稍微偏离。

@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

如果您需要XP解决方案,那么我认为您需要下载非本机命令行工具,该工具要求输入具有超时功能,或者切换到VBScript或JScript。

修改

上面的两个脚本都可以通过使用James K在答案中提供的CHOICE.EXE download from the Microsoft FTP site来适应在XP上运行。

该版本的CHOICE语法略有不同。

要调整我的第一个脚本,请使用:

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

要调整我的第二个脚本,请使用:

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


编辑 - 这是一个与XP兼容的原始VBS解决方案

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"

我认为您可以使用HTA提供更优雅的VBS解决方案,但这项工作要多得多,我对该技术的了解并不多。

答案 3 :(得分:0)

我会使用此代码使脚本暂停指定的毫秒数:

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

这将在1.1.1.1过去后向IP地址<milliseconds>发送1 ping。并且ping命令的输出将被解除给NUL。

答案 4 :(得分:0)

我建议SPC_75使用此脚本 它虽然是.vbs文件,但对于休眠,但它很容易修改为睡眠或关机。

'//*******************************************
'//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

与警告和超时完美配合。