Board logo

标题: 在批处理中,如何显示倒计时? [打印本页]

作者: martin325     时间: 2006-4-8 21:29    标题: 在批处理中,如何显示倒计时?
当用Choice设定延时命令时间后,如何在批处理中实现这个时间的倒计时显示?

请教了:)

作者: martin325     时间: 2006-4-8 22:33
顺计时也可:)

作者: martin325     时间: 2006-4-9 00:00
在网上搜索到的一个批处理如下:

以下面的格式运行LOOP.BAT逐一显示从1 到 9(注意参数之间要空格):
C:>LOOP 1 2 3 4 5 6 7 8 9

LOOP.BAT的内容:
代码:
@ECHO OFF
:LOOP
IF '%1'=='' GOTO END
SET COUNT=%1
CLS
ECHO %COUNT%
PAUSE
SHIFT
GOTO LOOP
:END

好像不够完整,执行不了!
而且从1 到 9逐一显示,也不妥。那样岂不是要显示10行!

作者: martin325     时间: 2006-4-9 12:43
最好有像Config.sys启动菜单所显示的倒计时。

不知哪位高手能解?

作者: chenhui530     时间: 2006-4-9 13:14
w.bat可以

作者: martin325     时间: 2006-4-9 13:27
最好是不用第三方工具。

用批处理来调用DOS本身的倒计时功能!

作者: willsort     时间: 2006-4-9 19:12
Re martin325:

不用第三方工具,目前有两个方案:

1、使用debug现场修改choice,曾经做过这方面的尝试,后来考虑对choice的修改过大而失去了debug的意义,同时过多的插入代码也可能会对程序的健壮性带来影响,最后放弃了;

2、利用3楼提到的choice循环来完成。代码如下:

不过,考虑到这两种方案复杂度都比较高,还是推荐使用第三方工具。


:: TimeOut.bat - Test of timeout
:: Will Sort - 18:52 2006-4-9 - MSDOS7.10/COMMAND@Win98
@echo off
if "%1"=="" %0 : 9 8 7 6 5 4 3 2 1
if "%1"=="" goto end

:TimeOutLoop
cls
echo Please choose 1~9: %2
choice /c123456789_ /t:_,1 > nul
if not errorlevel 10 goto Chosen
shift
if not "%2"=="" goto TimeOutLoop

:Chosen
for %%e in (1 2 3 4 5 6 7 8 9) do if errorlevel %%e set _e=%%e
if errorlevel 10 set _e=
echo You choice: %_e%
set _e=

:end


Last edited by willsort on 2006-4-10 at 12:28 ]

作者: martin325     时间: 2006-4-9 19:59
不知显示30秒倒计时,可否把
if "%1"=="" %0 : 9 8 7 6 5 4 3 2 1

改为
if "%1"=="" %0 : 29 28 27 26 …………

作者: martin325     时间: 2006-4-9 20:26
刚刚测试了,发现没有达到预期效果,屏幕提示:

Invalid time
Enter new time

作者: martin325     时间: 2006-4-10 00:00
为什么config.sys能用MSDOS自身的计时器,而批处理不能调用呢?

请高手研究一下!

作者: willsort     时间: 2006-4-10 12:28
Re martin325:

Originally posted by martin325 at 2006-4-9 20:26:
刚刚测试了,发现没有达到预期效果,屏幕提示:

Invalid time
Enter new time


根据提示来看,是错误使用了time命令,而我的代码未使用到time,那么可能的原因是,你将代码命名为time.bat(本名为TimeOut.bat)或者在其他代码中使用time时出错。

为什么config.sys能用MSDOS自身的计时器,而批处理不能调用呢?

config.sys的倒计时由io.sys提供支持,而io.sys则是用汇编实现,算法应该是反复读取系统时钟。DOS本身应该并未提供可供调用的倒计时显示中断或函数。因此,批处理仍然需要借用其它命令或程序中的类似功能来替代性实现。

作者: voiL     时间: 2006-4-10 12:53
我之前也曾在论坛上找过相关的资料,但没有找到现成的,所以就自己东拼西凑的"凑"了个出来...

在XP CMD(ver:5.1.2600)下测试通过...


@echo off
set /a i = 10
:Timeout
if %i% == 0 goto Next
setlocal
set /a i = %i% - 1
cls
echo 当前选择时间: %i%
call:Delay 1000此1000为延迟时间,以毫秒计算,,即1秒.作为引用下面延迟脚本的输入参数.
goto Timeout
goto End

:Next
cls & echo.
echo 恭喜!倒计时测试成功!
echo.
pause & exit

:End
echo 很抱歉!倒计时测试没有成功!
exit

::============================================================
:Delay
@echo off & setlocal enableextensions enabledelayedexpansion
echo WScript.Sleep %1 > %temp%.\tmp$$$.vbs
echo %time%>nul
cscript //nologo %temp%.\tmp$$$.vbs
echo %time%>nul
for %%f in (%temp%.\tmp$$$.vbs) do if exist %%f del %%f
endlocal & goto :EOF

作者: willsort     时间: 2006-4-10 13:11    标题: [求助]如何将键盘的输入字符直接送入某变量作为选择项
Re voiL:

你的倒计时代码没有实现选择后中断倒计时的功能,用户只能选择等待,直到倒计时结束。而如果使用set/p或实现等待选择,则倒计时将无法继续,这应该是cmd脚本的局限性,只有考虑使用32位版本的choice.exe了。

另外,感觉你的vbs脚本只需要生成一次就足够了,在调用前生成,最后一次调用完成后删除,应该可以提高不少效率。否则,倒计时的精确性可能会受到影响。这应该算作模块化编程的弊病了。另外,:End标签感觉将始终无法抵达,因为在goto end总是先goto Timeout。


@echo off & setlocal enableextensions
echo WScript.Sleep 1000 > %temp%.\tmp$$$.vbs
set /a i = 10
:Timeout
if %i% == 0 goto Next
setlocal
set /a i = %i% - 1
cls
echo 当前选择时间: %i%
cscript //nologo %temp%.\tmp$$$.vbs
goto Timeout
goto End

:Next
cls & echo.
echo 恭喜!倒计时测试成功!
echo.
for %%f in (%temp%.\tmp$$$.vbs*) do del %%f
pause & exit

作者: martin325     时间: 2006-4-10 20:38
这个循环批处理中有“CLS”,目的使“echo 当前选择时间: %i%”始终在屏幕同一地方显示,但就是这个清屏命令,把本人认为应该给使用者的一些屏幕提示也一起清除了,呵呵,这个问题该如何解决?

作者: martin325     时间: 2006-4-10 20:41
现在需要解决两个问题:

一是保留已经在屏幕上显示的必要信息与提示

二是让倒计时显示始终在屏幕同一处显示

作者: kcdsw     时间: 2006-4-10 20:48
呵呵 楼上说的是先前执行的结果吧? 如果要显示固定内容 多加个echo就成了

作者: martin325     时间: 2006-4-10 21:27
这个不成的,如果循环执行的话,每次都显示一遍,累啊,也会占用很多内存的吧!

倒计时30秒,那不是要显示30遍,呵呵……

作者: martin325     时间: 2006-4-11 08:42    标题: 能否把Clock修改到体积只在4K之内?
Clock 2.12
DOS下最好的TSR方式的时钟工具。它的功能相当多也非常强大,比如可以以多种格式(如12/24小时制,是否显示分/秒/毫秒等)显示,指定时钟在屏幕上的显示位置(默认在屏幕的右上角),还可随时在命令行上启用/禁用/退出等。

请高手修改Clock 2.12,使其体积只在4K之内,功能只要倒计时(或顺计时),并在屏幕任意指定的位置显示。

先谢谢了:)
附件 1: CLOCK.rar (2006-4-11 08:42, 5.42 KiB, 下载附件所需积分 1点 ,下载次数: 40)

作者: Climbing     时间: 2006-4-11 09:37
似乎Horst的wait4程序更好用。

------------------------------------------------------------------------
WAIT4 Batch: Wait for ..... Ver 1.4 (c) 1996 Horst Schaeffer
------------------------------------------------------------------------

Syntax: WAIT4 hh:mm:ss

WAIT4 waits according to given time and displays a "digital clock" as
a pop-up window - the ground is restored after the time has elapsed.
Abort by any key (optional: ESC only).

Time display/specification always in 24 hour mode (no am/pm).

Time specification:

(1) Target time in the form hh:mm:ss, where seconds and minutes
are optional, examples:

WAIT4 12:15 means: WAIT4 12:15:00
WAIT4 20 means: WAIT4 20:00:00

(2) Period of time with preceeding "+". Here the seconds are
mandatory, and minutes/hours may be omitted.

WAIT4 +12:15 means: WAIT4 + 00:12:15
WAIT4 +20 means: WAIT4 + 00:00:20

(3) no specification: Clock runs until stopped by keystroke

For (1) and (2) the target time is shown under the clock.

Options:

/O Over midnight:
If the given time has passed already, the program is terminated
instantly (errorlevel 250, see below), unless you have specified
by option /O, that the given time is meant for the next day.

If you specify a period of time, option /O is not necessary.

Option /O is ignored, if the given time has not yet been reached
on the same day, because WAIT4 will wait a maximum of 24 hours.

/D CountDown display instead of current time.

/E ESC: abort with ESC only, other keys ignored.

/B Bell after time elapsed.

/Pl,c Position:
The position of the clock may be given by line and column,
both counting from 1. Specifications will be corrected if the
display window would not fit on the screen (current display
mode checked). Default line=current, column=32.

/Rn Random position on screen, changes every n seconds

/S Screen to clear at start (not restored at end)

/Cn Color:
Color code n can be obtained by addition of following color
values (default: 79 = bright white on red):

character: background:
0 0 black
1 16 blue
2 32 green
3 48 cyan
4 64 red
5 80 magenta
6 96 brown/yellow
7 112 white
+8 bright

Example: bright green on magenta
= 8 + 2 + 80 = 90

Errorlevel:

0 Time elapsed
1-199 aborted by ASCII key with code 1..199
200 aborted by any other key
250 target time passed already at start of program
255 invalid command arguments


Digit fonts to patch:

The file WAIT4.PAT is a DEBUG script to patch the digit fonts.
Use an editor to copy the section with the desired font into the
script (or make your own digits). Then, on the command line, enter:

DEBUG WAIT4.COM < WAIT4.PAT


Note: Time slices are released under WINDOWS, OS/2 and DV.

----
WAIT4 is freeware by Horst Schaeffer - no warranties of any kind
eMail: horst.schaeffer@gmx.net

= 04 AUG 2002

作者: martin325     时间: 2006-4-11 09:42
哪儿有下载wait4 ?

作者: martin325     时间: 2006-4-11 16:03
找到Horst的主页,可是没发现有wait4程序啊!

http://home.mnet-online.de/horst.muc/index.html

作者: martin325     时间: 2006-4-12 22:15
请Climbing
上传WAIT4

作者: martin325     时间: 2006-4-13 20:09
还是自力更生,呵呵,写了一封简短的信给作者。
作者给我回信如下:
Hello,

Wait4 is available on my website in the
PBats package.
See Menu on the left, "Horst's PBats"

终于找到了,呵呵,现在上传,给大家共享。
附件 1: pbats32.zip (2006-4-13 20:09, 59.58 KiB, 下载附件所需积分 1点 ,下载次数: 58)

作者: Climbing     时间: 2006-4-14 09:30
是你自己没有好好找,人家的网站上说得很清楚的。

作者: martin325     时间: 2006-4-14 10:10
不是没好好找,而是人家的网站上不是总是提供下载的!
刚刚我还特意去看了看,那个下载说明和联结又没有了!
不信的话,你现在就去看看:http://home.mnet-online.de/horst.muc/index.html
点击左边菜单的Horst's Pbats,总是显示原来的网页,也没有WAIT4的说明!

作者: qzwqzw     时间: 2006-4-14 11:34
有啊,就在左边导航栏里,有pbat的链接,不过好像是框架结构的网页,所以直接输入网页链接不太管用。不过pbat的下载链接可以获得:
http://home.mnet-online.de/horst.muc/int/pbats32.zip

作者: willsort     时间: 2006-4-14 18:27
Originally posted by martin325 at 2006-4-10 21:27:
这个不成的,如果循环执行的话,每次都显示一遍,累啊,也会占用很多内存的吧!

倒计时30秒,那不是要显示30遍,呵呵……


Re martin325:

30秒的倒计时确实可能会显示30遍屏幕信息,但是这并不会显著影响批处理代码的运行效率。对于echo之类的输入输出命令,系统特别是BIOS层有自己独特的处理方式,以保证其高效性。因此在系统资源和时间的耗费上,将一屏信息显示30遍并不能简单地叠加为显示1遍的30倍。

实际测试中,cls后再echo并不会给倒计时带来可以察觉的误差,而由于cls所可能带来的刷屏闪烁问题,也只有程序处在软盘等访问速度较低的介质上时才会出现。

当然,这并非鼓励你使用pure batch进行代码编写,在条件允许的前提下使用第三方工具,确实可以方便的解决许多问题,并同时减少代码的编写量和复杂度。但是,如果你是为群体用户编写代码,那么就必须考虑是否可以付出第三方工具所需要的代价,比如兼容性、安全性、版权问题等等。

作者: appleliuyg     时间: 2006-4-17 14:15    标题: 60秒倒计时显示
for /l %a in (60,-1,1) do echo %a&&choice /t 1 /d y /n

以测过。

作者: martin325     时间: 2006-4-17 20:13
呵呵,在DOS下测试,没通过!

作者: awtq     时间: 2007-5-9 11:01
60秒倒计时執行命令要怎樣做??

作者: ydyxy1234     时间: 2007-5-9 20:53
学到一点,,呵呵

作者: 26933062     时间: 2007-5-17 01:33
学习

Last edited by 26933062 on 2007-5-26 at 12:04 AM ]

作者: zhuyongfu     时间: 2009-4-17 02:43
timeout 60

这个是NT下命令,如果要在纯DOS下实现的话可能要用到第三方的工具,例如楼上说到的wait.exe 等,如果懂Basic的话,可以编一个小程序啊,应该不是很难实现吧?

作者: wuwei1dai     时间: 2009-4-18 01:59
@echo off
echo 你要固定显示的内容
for /l %%i in (30,-1,0) do (
ping -n 2 127.1>nul
if %%i geq 10 (curs /pos 5 10&echo %%i) else (curs /pos 5 10&echo 0%%i)
CurS /crv 0
)


其中curs 5 10中的5 10是显示倒计时的坐标
curs.exe很好找的,如果找不到我用QQ发给你459253744