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

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

问题描述

我正在尝试编写批处理脚本并尝试在 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.

注意:这不是另一个问题在批处理文件中睡眠的完整副本也是关于python的,而这是关于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 doesn't exist and specify the desired timeout:

ping 192.0.2.2 -n 1 -w 10000 > nul

由于地址不存在,它将等待 10,000 毫秒(10 秒)并返回.

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

  • -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 try 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 并使用上述技术轻松地自己创建 sleep 命令:

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

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

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


注意(2002 年 9 月): 192.0.2.x 地址是根据 RFC 3330 所以它肯定不会存在于现实世界中.引用规范:


NOTE (September 2002): 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";用于文档和示例代码.它经常与供应商和协议中的域名 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天全站免登陆