如何在批处理脚本等待? [英] How to wait in a batch script?

查看:171
本文介绍了如何在批处理脚本等待?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:

睡在DOS批处理文件

我试图写一个批处理脚本,并试图等待2函数调用之间的10秒。命令:

I am trying to write a batch script and trying to wait 10 seconds between 2 function calls. The command:

sleep 10

不使批处理文件等待10秒。

Does not make the batch file wait for 10 seconds.

我运行Windows XP。

I am running Windows XP.

注意:这不是睡觉的批处理文件因为其他问题的完整副本大约也是关于蟒蛇,虽然这是有关Windows批处理文件。

Note: This is not a complete duplicate of Sleeping in a batch file as the other question is about also about python, while this is about windows batch files.

推荐答案

您可以ping这肯定不存在,并指定所需的超时地址:

You can ping an address that surely doesn't exist and specify the desired timeout:

ping 192.0.2.2 -n 1 -w 10000 > nul

此外,由于地址不存在,它会等待万毫秒(10秒),并返回。

And since the address does not exists, it'll wait 10,000 ms (10 seconds) and returns.


  • -w 10000 部分指定以毫秒为单位需要的超时。

  • -n 1 部分告诉ping只应尝试的一次的(通常它会尝试4次)。

  • > NUL 部分追加所以ping命令无法正常输出到屏幕上。

  • The -w 10000 part specifies the desired timeout in milliseconds.
  • The -n 1 part tells ping that it should only tries once (normally it'd try 4 times).
  • The > nul part is appended so the ping command doesn't output anything to screen.

您可以轻松地让自己的睡眠命令通过在你的PATH的地方创造一个sleep.bat并使用上述技术:

You can easily make a sleep command yourself by creating a sleep.bat somewhere in your PATH and use the above technique:

rem SLEEP.BAT - sleeps by the supplied number of seconds

@ping 192.0.2.2 -n 1 -w %1000 > nul


注意:的192.0.2.x地址保留为每 RFC 3330 所以它绝对不会在现实世界中存在。从规格报价:


NOTE: The 192.0.2.x address is reserved as per RFC 3330 so it definitely will not exist in the real world. Quoting from the spec:

192.0.2.0/24 - 该块被指定为TEST-NET用于
  文档和示例code。它经常被一起使用
  域名example.com或供应商和协议example.net
  文档。此块中的地址不应该出现在
  公共互联网。

192.0.2.0/24 - This block is assigned as "TEST-NET" for use in documentation and example code. It is often used in conjunction with domain names example.com or example.net in vendor and protocol documentation. Addresses within this block should not appear on the public Internet.

这篇关于如何在批处理脚本等待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆