If statement 如何请求具有超时的批处理文件用户输入

If statement 如何请求具有超时的批处理文件用户输入,if-statement,batch-file,timer,shutdown,restart,If Statement,Batch File,Timer,Shutdown,Restart,因此,我试图在set命令下的if语句中添加一个计时器,但我不确定该命令是什么。脚本将启动并等待30分钟,然后重新启动电脑,或者等待用户输入或取消。因此,我有两个if语句用于设置“立即重新启动”和“取消”,但现在我需要一个if语句,使其在执行restart命令前三十分钟倒计时。另外,如果有人知道如何在上面添加一个显示剩余时间的可视计时器,那将是一个加号。谢谢大家 @Echo off :START set /p answer=PC WILL RESTART IN 30 MINUTES, PRES

因此,我试图在set命令下的if语句中添加一个计时器,但我不确定该命令是什么。脚本将启动并等待30分钟,然后重新启动电脑,或者等待用户输入或取消。因此,我有两个if语句用于设置“立即重新启动”和“取消”,但现在我需要一个if语句,使其在执行restart命令前三十分钟倒计时。另外,如果有人知道如何在上面添加一个显示剩余时间的可视计时器,那将是一个加号。谢谢大家

@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

我将使用以下代码使脚本暂停指定的毫秒数:

PING 1.1.1.1 -n 1 -w <milliseconds> >NUL 
ping1.1.1.1-n1-w>NUL

这将在
之后向IP地址
1.1.1
发送1个ping。并且,
ping
命令的输出将被取消为NUL。

这是一个非常简单的解决方案,适用于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和Windows7的更复杂的解决方案,它提供了一个可视化倒计时,但它每秒都会清除控制台窗口。而且时间可能有点不合适

@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在其答案中提供的脚本来适应在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. "
要调整第二个脚本,请使用:

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"
Set objShell=CreateObject(“WScript.Shell”)
对于i=30到1步骤-1
如果i=1,则单位为分钟,否则单位为分钟
rtn=objShell.Popup(_
“计算机要重新启动。”&VbCrLf&VbCrLf&_
“单击“确定”立即重新启动”&VbCrLf&_
“单击取消或[X]关闭按钮中止重新启动”&VbCrLf&_
“不执行任何操作,机器将在”&i&unit中重新启动_
60,“重新启动”和仪表及装置,1+48_
)
如果rtn为-1,则退出
下一个
如果是rtn 2,则运行“shutdown-r-f”

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

我建议使用
CHOICE.EXE
,它是大多数Windows版本的标准配置(除了Windows NT、2000和XP之外,它过去可以从微软的网站上下载,但他们似乎忽略了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]?
…两者都将等待用户按下
Y
N

选项将结果存储在%errorlevel%.Y=1,N=2中

我提供的代码利用了默认的
/D
和超时
/T
选项

例如

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
没有错误级别0

有关使用
choice
的详细信息,请在命令提示下键入
choice/?



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

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

我推荐SPC_75的这个脚本 虽然它是一个.vbs文件,用于hibernate,但对于睡眠或关机,它很容易修改

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

警告和超时都能正常工作。

有没有办法不用choice命令就可以做到这一点?我的电脑大部分都是XP机器,我真的不想从Microsoft下载choice命令。@MattP-正如我在回答中所说的,你可以使用VBS,但不能使用本机批处理。请参阅我更新的answer用于VBS解决方案。这有什么帮助?OP需要提示用户输入,如果用户在超时时间内没有响应,则会自动使用默认值。唯一执行此操作的本机批处理命令是CHOICE,但这不是XP的本机命令。在我回答了他们最初的问题后,他们更改了要求。您能解释一下这是如何实现的吗工作?顺便说一句,冷却倒计时。@workoverflow用于循环倒计时,从600秒开始,一次倒-1,直到1。从秒开始计算分和秒。只需用0填充秒。分和秒被传递到选择字符串。
/c:CN1
是将c和N字符从输入映射到
错误级别
。如果错误级别为1或2,用户按ke键y,然后跳开。错误等级2是N,所以冬眠。