2007/11/28

使用 ping 達到 sleep 的效果

作者 depository (de) 看板 EZsoft
標題 batch file - sleep
時間 Mon Nov 26 19:57:22 2007
───────────────────────────────────────

話說在 windows batch file 裡面寫東西超多限制的,例如 for 裡面不能用 if
(光是這點己經頭大), date 又秀逗,` ` 裡面能放的東面又不多...

回正題, 話說以前遍尋不著的 sleep (讓電腦暫停某段時間的指令), 終於在
windows server 2003 resource kit tools 裡找到,而google 一下之後發現ping
127.0.0.1 也可以有相似的效果。


Implementing the WAIT Command in a Batch File
SUMMARY: Make your batch files wait or sleep for a specified number of
seconds before continuing. Works in DOS and Windows.

Do you need a batch file that waits a certain amount of seconds? In some
languages, the command would be WAIT, but DOS and Windows do not come with
this command for batch files. If you want to implement the WAIT command:

For DOS, Windows 95, and Windows 98 machines

Create the following batch file and name it WAIT.BAT.

@CHOICE /T:N,%1% > NUL
Now, in order to wait 10 seconds in a batch file, just call the WAIT.BAT
batch file as follows:

CALL WAIT 10

For Windows 95, Windows 98, Windows NT, Windows 2000, Windows XP, or Windows
2003 machines

You can use the following WAIT.BAT, as the CHOICE command is not included in
the default install on Windows NT, Windows 2000, Windows XP, or Windows 2003:

@ping 127.0.0.1 -n 2 -w 1000 > nul
@ping 127.0.0.1 -n %1% -w 1000> nul

The reason for the two lines is that in order to wait using the PING command,
you must add extra pings to correctly wait the desired number of seconds.
Also, since the PING command is being used, it is possible that this WAIT
batch file may run over a few milliseconds. Thus, it is not recommended to
use this for critical real-time processing.

Windows XP and Windows 2003 machines

Since Windows XP and 2003 users do not have the CHOICE command, if you find
the PING alternative not to your liking, the Windows 2003 Resource Kit has a
batch file SLEEP command.


--
※ 發信站: 批踢踢實業坊(ptt.cc)

沒有留言: