批量倒计时但显示倒计时
Batch countdown but WITH showing countdown
我知道它可以批处理超时 30 等但是如何显示
的倒计时
我看到这里:
http://www.trytoprogram.com/batch-file/shutdown-commands/
底部页面“用于关闭、重新启动、休眠和注销计算机的批处理文件程序”
/t 0 从来没有用过 /t 30 它没有显示倒计时
知道如何像这样显示倒计时:超时 30 29 28 27 等
计时器可以通过使用 for /L
秒数循环和 for /F
循环执行 choice
命令和 /T 1
延迟来实现/D
默认不执行任何操作。
可以在 For /F Choice
循环中内置其他选项,以允许取消关机、重置倒计时或立即关机:
@Echo off
:Reset
For /L %%i in (30 -1 1)Do (
Cls
Echo Shutting down in : %%i Seconds [C]ancel [S]hutdown Now [R]eset Timer
For /F "Delims=" %%G in ('Choice /T 1 /N /C:CSRW /D W')Do (
If %%G==R Goto :Reset
If %%G==C Goto :Eof
If %%G==S Shutdown /SG /T 0
)
)
Shutdown /SG /T 1
编辑:
修改以上内容以用作从菜单调用的函数的示例。
@Echo off
:Menu
Echo [E]xit [L]ogoff [R]estart [S]hutdown
For /F "Delims=" %%G in ('Choice /N /C:SRLE')Do (
If %%G==S Call :Timer "Shutdown /SG /T 1" "Shutting Down"
If %%G==R Call :Timer "Shutdown /R /T 1" "Restarting"
If %%G==L Call :Timer "Shutdown /L" "Logging Off"
If %%G==E Exit /B
)
:# On Cancel Command Selection:
Goto :Menu
:# Function With timer
:Timer [Command] [Prompt]
For /L %%i in (30 -1 1)Do (
Cls
Echo %~2 in : %%i Seconds [C]ancel [R]estart Timer [N]o wait
For /F "Delims=" %%G in ('Choice /T 1 /N /C:CRNW /D W')Do (
If %%G==R Goto :Timer
If %%G==C Goto :Eof
If %%G==N %~1
)
)
%~1
PsShutdown
您可以使用 Mark Rusinovich 的 psshutdown,它完全符合您的要求:
- 在指定时间后启动关机
- 显示倒计时
- 允许用户使用 取消 按钮取消
免费使用。
命令行:psshutdown -k -f -t 30 -c
-k
关闭计算机(ACPI 电源关闭)
-f
必要时强制程序终止
-t 30
在关机前引入 30 秒的延迟
-c
允许用户使用 取消 按钮取消关机
(来自 superuser.com 的屏幕截图)
选择
另一种选择是多次调用 choice /t 1
:
@echo off
set /a "t=30"
:loop
set /a "t-=1"
if "%t%" == "0" goto timedout
cls
choice /T 1 /C sc /N /D s /M "Shutdown in %t% seconds, press c to cancel: "
if not "%errorlevel%" == "1" goto cancelled
goto loop
:cancelled
echo You cancelled shutdown
pause
goto :eof
:timedout
echo Shutting down
shutdown /s /f /t 1
pause
goto :eof
set /a "t=30"
将变量 t
设置为 30
set /a "t-=1"
从 t
中减去 1
if "%t%" == "0" goto timedout
在 t
达到零时启动关机
cls
从屏幕上清除之前的文本
choice
是 timeout
知道它是否被取消但没有打印时间
/T 1
- 1 秒后超时(更新消息)
/C sc
- 选择 1 是 s,选择 2 是 c
/N
- 最后不显示[s,c]
/D s
- 1 秒超时的默认选项是 s
/M "Shutdown in %t% ..."
- 显示带有变量当前值的消息 t
请注意,shutdown /s /f /t NNN
的行为可能与 shutdown /s /f
不一样,所以我包含了 /t 1
以防万一。
关机/a
在倒计时之前实际启动关闭可能是有利的,但允许用户中止它:
按照上一节(“选择”)中的说明进行倒计时。
- 你可以用简单的
timeout
命令再过几秒钟,希望批处理脚本在时间用完之前被关闭中止,但这会非常混乱和不可靠,所以这是不推荐
在开始倒计时 (:loop
) 之前像这样开始关机:
shutdown /s /f /t 30
如果超时,什么也不做(在timedout:
之后删除shutdown /s /f /t 1
)
如果用户取消(在 :cancelled
之后),中止它:shutdown /a
我知道它可以批处理超时 30 等但是如何显示
的倒计时我看到这里: http://www.trytoprogram.com/batch-file/shutdown-commands/ 底部页面“用于关闭、重新启动、休眠和注销计算机的批处理文件程序”
/t 0 从来没有用过 /t 30 它没有显示倒计时
知道如何像这样显示倒计时:超时 30 29 28 27 等
计时器可以通过使用 for /L
秒数循环和 for /F
循环执行 choice
命令和 /T 1
延迟来实现/D
默认不执行任何操作。
可以在 For /F Choice
循环中内置其他选项,以允许取消关机、重置倒计时或立即关机:
@Echo off
:Reset
For /L %%i in (30 -1 1)Do (
Cls
Echo Shutting down in : %%i Seconds [C]ancel [S]hutdown Now [R]eset Timer
For /F "Delims=" %%G in ('Choice /T 1 /N /C:CSRW /D W')Do (
If %%G==R Goto :Reset
If %%G==C Goto :Eof
If %%G==S Shutdown /SG /T 0
)
)
Shutdown /SG /T 1
编辑:
修改以上内容以用作从菜单调用的函数的示例。
@Echo off
:Menu
Echo [E]xit [L]ogoff [R]estart [S]hutdown
For /F "Delims=" %%G in ('Choice /N /C:SRLE')Do (
If %%G==S Call :Timer "Shutdown /SG /T 1" "Shutting Down"
If %%G==R Call :Timer "Shutdown /R /T 1" "Restarting"
If %%G==L Call :Timer "Shutdown /L" "Logging Off"
If %%G==E Exit /B
)
:# On Cancel Command Selection:
Goto :Menu
:# Function With timer
:Timer [Command] [Prompt]
For /L %%i in (30 -1 1)Do (
Cls
Echo %~2 in : %%i Seconds [C]ancel [R]estart Timer [N]o wait
For /F "Delims=" %%G in ('Choice /T 1 /N /C:CRNW /D W')Do (
If %%G==R Goto :Timer
If %%G==C Goto :Eof
If %%G==N %~1
)
)
%~1
PsShutdown
您可以使用 Mark Rusinovich 的 psshutdown,它完全符合您的要求:
- 在指定时间后启动关机
- 显示倒计时
- 允许用户使用 取消 按钮取消
免费使用。
命令行:psshutdown -k -f -t 30 -c
-k
关闭计算机(ACPI 电源关闭)-f
必要时强制程序终止-t 30
在关机前引入 30 秒的延迟-c
允许用户使用 取消 按钮取消关机
选择
另一种选择是多次调用 choice /t 1
:
@echo off
set /a "t=30"
:loop
set /a "t-=1"
if "%t%" == "0" goto timedout
cls
choice /T 1 /C sc /N /D s /M "Shutdown in %t% seconds, press c to cancel: "
if not "%errorlevel%" == "1" goto cancelled
goto loop
:cancelled
echo You cancelled shutdown
pause
goto :eof
:timedout
echo Shutting down
shutdown /s /f /t 1
pause
goto :eof
set /a "t=30"
将变量t
设置为 30set /a "t-=1"
从t
中减去 1
if "%t%" == "0" goto timedout
在t
达到零时启动关机cls
从屏幕上清除之前的文本choice
是timeout
知道它是否被取消但没有打印时间/T 1
- 1 秒后超时(更新消息)/C sc
- 选择 1 是 s,选择 2 是 c/N
- 最后不显示[s,c]
/D s
- 1 秒超时的默认选项是s
/M "Shutdown in %t% ..."
- 显示带有变量当前值的消息t
请注意,shutdown /s /f /t NNN
的行为可能与 shutdown /s /f
不一样,所以我包含了 /t 1
以防万一。
关机/a
在倒计时之前实际启动关闭可能是有利的,但允许用户中止它:
按照上一节(“选择”)中的说明进行倒计时。
- 你可以用简单的
timeout
命令再过几秒钟,希望批处理脚本在时间用完之前被关闭中止,但这会非常混乱和不可靠,所以这是不推荐
- 你可以用简单的
在开始倒计时 (
:loop
) 之前像这样开始关机:shutdown /s /f /t 30
如果超时,什么也不做(在
timedout:
之后删除shutdown /s /f /t 1
)如果用户取消(在
:cancelled
之后),中止它:shutdown /a