在批处理文件中睡觉 [英] Sleeping in a batch file

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

问题描述

在 Windows 机器上编写批处理文件以自动执行某些操作时,我需要暂停其执行几秒钟(通常在测试/等待循环中,等待进程启动).当时,我能找到的最好的解决方案是使用 ping(我不骗你)来达到预期的效果.我在这里找到了一篇更好的文章,它描述了一个可调用的wait.bat",实现如下:

When writing a batch file to automate something on a Windows box, I've needed to pause its execution for several seconds (usually in a test/wait loop, waiting for a process to start). At the time, the best solution I could find uses ping (I kid you not) to achieve the desired effect. I've found a better write-up of it here, which describes a callable "wait.bat", implemented as follows:

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

然后,您可以在自己的批处理文件中包含对 wait.bat 的调用,传入休眠的秒数.

You can then include calls to wait.bat in your own batch file, passing in the number of seconds to sleep.

显然 Windows 2003 Resource Kit 提供了一个类 Unix 的睡眠命令(终于!).与此同时,对于我们这些仍在使用 Windows XP、Windows 2000 或(遗憾的是)Windows NT,有没有更好的方法?

Apparently the Windows 2003 Resource Kit provides a Unix-like sleep command (at last!). In the meantime, for those of us still using Windows XP, Windows 2000 or (sadly) Windows NT, is there a better way?

我修改了 sleep.py 脚本>接受答案,因此如果命令行上没有传递参数,则默认为一秒:

I modified the sleep.py script in the accepted answer, so that it defaults to one second if no arguments are passed on the command line:

import time, sys

time.sleep(float(sys.argv[1]) if len(sys.argv) > 1 else 1)

推荐答案

UPDATE

timeout 命令(适用于 Windows Vista 及更高版本)应该是所使用的命令,如另一个中所述回答这个问题.下面是一个答案.

UPDATE

The timeout command, available from Windows Vista and onwards should be the command used, as described in another answer to this question. What follows here is an old answer.

如果您安装了 Python,或者不介意安装它(它也有其他用途:),只需创建以下 sleep.py 脚本并将其添加到 PATH 中的某个位置:

If you have Python installed, or don't mind installing it (it has other uses too :), just create the following sleep.py script and add it somewhere in your PATH:

import time, sys

time.sleep(float(sys.argv[1]))

如果您有这样的需要,它将允许亚秒级暂停(例如,1.5 秒、0.1 秒等).如果您想将其称为 sleep 而不是 sleep.py,那么您可以将 .PY 扩展名添加到您的 PATHEXT 环境变量中.在 Windows XP 上,您可以在以下位置进行

It will allow sub-second pauses (for example, 1.5 sec, 0.1, etc.), should you have such a need. If you want to call it as sleep rather than sleep.py, then you can add the .PY extension to your PATHEXT environment variable. On Windows XP, you can edit it in:

我的电脑→属性(菜单)→高级(选项卡)→环境变量(按钮)→系统变量(框架)

My Computer → Properties (menu) → Advanced (tab) → Environment Variables (button) → System variables (frame)

这篇关于在批处理文件中睡觉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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