batch-file - 如何在批处理脚本中等待?

标签 batch-file

我正在尝试编写批处理脚本并尝试在 2 个函数调用之间等待 10 秒。命令:

sleep 10

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

我运行的是 Windows XP。

注意:这不是 Sleeping in a batch file 的完整副本。因为另一个问题也与 python 相关,而这是关于 Windows 批处理文件。

最佳答案

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

ping 192.0.2.2 -n 1 -w 10000 > nul

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

  • -w 10000 部分指定所需的超时(以毫秒为单位)。
  • -n 1 部分告诉 ping 它应该只尝试一次(通常会尝试 4 次)。
  • 附加 > nul 部分,以便 ping 命令不会向屏幕输出任何内容。

您可以通过在 PATH 中的某个位置创建 sleep.bat 并使用上述技术来轻松地自己创建 sleep 命令:

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

@ping 192.0.2.2 -n 1 -w %1000 > nul
<小时/>

注意(2020 年 9 月): 192.0.2.x 地址根据 RFC 3330 保留。所以现实世界中绝对不会存在。引用规范:

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.

关于batch-file - 如何在批处理脚本中等待?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/735285/

相关文章:

powershell - 批处理脚本期间重启环境和脚本

batch-file - 批量检测键盘输入

windows - 批处理以列出不包含特定文件的文件夹

windows - 找出批处理文件在多少个父进程下运行

java - 如何在 Linux shell 中接受左/右/上/下箭头输入?

XML : tools for re-indenting XML in batch mode

string - 如何在特定单词后剪切字符串?

variables - 批处理 - 在另一个变量中回显变量的值?

batch-file - 递归 .bat 文件将 jpegtran 和 pngout 应用于子文件夹中的所有图像

batch-file - 在命令行不可见的情况下执行批处理文件