在超时命令中仅显示倒计时

在超时命令中仅显示倒计时

我需要运行超时命令而不显示消息press a key to continue...

我尝试了该命令timeout /t 10 > null,但它对我来说不起作用,因为它将消息和倒计时一起删除。

默认命令:

@echo off
echo Wait...
timeout /t 10
exit

结果:

Wait...
Waiting 9 seconds, press a key to continue...

我需要的:

Wait...
Waiting 9 seconds

如何使超时命令仅显示倒计时而不显示消息press a key to continue ...

答案1

如果您不想要倒数计时器,您应该接受@Hackoo 的帖子。他们首先回答了大致相同的答案,但没有实际的 10 .. 9 .. 8 .. 等。

如果您确实想要一个真正的倒计时器,那么如果不使用 ANSI 转义序列来定位光标就会变得很棘手。

这是一种简单的方法,即每次清除屏幕(您可能不喜欢它)。

@echo off
set countdown=10

:timer_loop
cls
echo Wait...
echo Waiting %countdown% seconds...

timeout /t 1 /nobreak >nul

set /a countdown=%countdown% -1
if %countdown% NEQ 0 goto :timer_loop

echo Timer complete.

答案2

基本上是 Señor CMasMas 的答案,但使用技巧来覆盖一行而不是清除整个屏幕:

@echo off
setlocal EnableDelayedExpansion
REM create a CariageReturn:
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
set countdown=10
:timer_loop
<nul set /p ".=Waiting %countdown% seconds...  !cr!"
timeout /t 1 /nobreak >nul
set /a countdown-=1
if %countdown% GEQ 0 goto :timer_loop
echo Timer complete.           (enough spaces here to overwrite the timer line)

答案3

批量是nul并且不是null


@echo off
echo Wait...
timeout /t 10 /nobreak >nul
exit

进一步阅读暂停 /?


编辑 :仅显示进度条模拟,不显示消息press a key to continue...


也许这并不能回答 OP 的问题,但是如果其他人想要类似进度条模拟的东西,为什么不呢?


@echo off
Title Show only a simulation of Progress Bar without the message press a key to continue ...
CHCP 65001>nul
set countdown=10
echo. & Call :PSColor "Please Wait a while ... Working is in Progress ..." DarkCyan \n
:timer_loop
REM Remark : I got "◘" by pressing [Alt] + [8]
REM (the actual symbol may vary depending upon codepage).
<nul set /p "_s=◘"
timeout /t 1 /nobreak >nul
set /a countdown-=1
if %countdown% GEQ 0 goto :timer_loop
echo. & Call :PSColor "Timer is completed." Cyan \n
Pause & Exit /B
::----------------------------------------------------------------
 :PSColor <String> <Color> <NewLine>
 If /I [%3] EQU [\n] (
    Powershell Write-Host "`0%~1" -ForegroundColor %2
 ) Else (
    Powershell Write-Host "`0%~1" -ForegroundColor %2 -NoNewLine
 )
 Exit /B
 ::---------------------------------------------------------------

相关内容