Mat*_*t P 6 if-statement shutdown restart timer batch-file
所以我试图在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
Run Code Online (Sandbox Code Playgroud)
我建议使用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
Run Code Online (Sandbox Code Playgroud)
只要CHOICE.EXE是这样的......
choice
Run Code Online (Sandbox Code Playgroud)
......和......一样
choice /c yn
Run Code Online (Sandbox Code Playgroud)
......都会显示......
[Y,N]?
Run Code Online (Sandbox Code Playgroud)
......两者都会等待用户按Y或N.
Choice将结果存储在%errorlevel%中.Y = 1,N = 2.
我提供的代码利用了默认/D <choice>和超时/T <seconds>选项.
在例子中......
choice /c yn /d y /t 5
Run Code Online (Sandbox Code Playgroud)
...给用户选择Y或N,等待5秒然后自动选择Y的默认选项,得到%ERRORLEVEL%== 1.
另一个例子是......
choice /c abcdef /m "Make a choice. "
Run Code Online (Sandbox Code Playgroud)
......它显示......
Make a choice. [A,B,C,D,E,F]?
Run Code Online (Sandbox Code Playgroud)
...和...
A = %ERRORLEVEL% = 1
B = %ERRORLEVEL% = 2
C = %ERRORLEVEL% = 3
D = %ERRORLEVEL% = 4
E = %ERRORLEVEL% = 5
F = %ERRORLEVEL% = 6
Run Code Online (Sandbox Code Playgroud)
没有ERRORLEVEL 0.
有关使用的更多信息choice,请CHOICE /?在命令提示符下键入.
*注意CHOICE.EXE我的版本提供了一个链接,使用略有不同的命令,但提供相同的功能.
类似的一个用于休眠.
@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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22809 次 |
| 最近记录: |