问题描述
当编写一个批处理文件来在Windows中自动执行一些东西,我需要暂停其执行几秒钟(通常在测试/等待循环,等待一个进程来启动)。当时,我能找到的最好解决方案采用平(我不骗你),以达到预期的效果。我发现它的一个更好的写了href=\"http://malektips.com/dos0017.html\"> ,它描述了一个可调用wait.bat,实现如下:
@ping 127.0.0.1 -n 2 -w 1000 GT; NUL
@ping 127.0.0.1 -n%1%-w 1000 GT; NUL
您就可以调用包含在自己的批处理文件来wait.bat,通过在几秒钟睡觉的数量。
(最后!)。在此期间,对于我们这些仍在使用Windows  XP,Windows 2000或(黯然),有没有更好的办法?
我在的,因此,它缺省值为1秒,如果没有参数在命令行上传递:
导入时间,SYStime.sleep(浮点(sys.argv中[1])如果len(sys.argv中)→1其他1)
如果您已经安装了Python,或者不介意安装它(它有其他用途太:),只需创建以下的 sleep.py 脚本某处添加在PATH:
导入时间,SYStime.sleep(浮点(sys.argv中[1]))
这将使分一秒的停顿(例如1.5秒,0.1等),你应该有这样的需要。如果你要称呼其为睡眠
,而不是 sleep.py
,那么你可以添加的.py
延伸到你的PATHEXT环境变量。在XP中,你可以编辑:
我的电脑→属性(菜单)→高级(卡)→环境变量(按钮)→系统变量(帧)
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
You can then include calls to wait.bat in your own batch file, passing in the number of seconds to sleep.
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?
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)
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]))
It will allow sub-second pauses (e.g. 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. In XP, you can edit it in:
My Computer → Properties (menu) → Advanced (tab) → Environment Variables (button) → System variables (frame)
这篇关于睡在一个批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!