Movatterモバイル変換


[0]ホーム

URL:


Sorry, we no longer support your browser
Please upgrade toMicrosoft Edge,Google Chrome, orFirefox. Learn more about ourbrowser support.
Skip to main content
Stack Overflow
  1. About
  2. For Teams
Loading…
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

You are notlogged in. Your edit will be placed in a queue until it ispeer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Sleeping in a batch file

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 ithere, 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 thesleep.py script in theaccepted answer, so that it defaults to one second if no arguments are passed on the command line:

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

Answer*

Draft saved
Draft discarded
Cancel
14
  • 15
    This should be accurate, but it is a CPU intensive way to go about it. Normally you want an interupt driven implementation so that the process does not consume CPU cycles while it waits. This shouldn't be a problem for infrequent short waits, as is probably the case for most batch situations. But a continuously running process with long waits could have a negative impact on system performance.CommentedFeb 15, 2012 at 21:43
  • 4
    As it uses the system clock the accuracy is one second. If 2 seconds is specified, the delay can be anything between 2 seconds and 3 seconds (actually 2.99999... seconds).CommentedAug 15, 2012 at 6:49
  • What if the time is now 7:59:59 and you want to wait 7 seconds? This looks like it doesn't take that into account.CommentedSep 28, 2012 at 18:45
  • This consumes more CPU time than it counts system time. In addition you are doing arithmetic operations with octal numbers, since minutes and seconds smaller than 10 are displayed with a leading 0 in theTIME command.CommentedMar 1, 2013 at 15:32
  • 2
    @mrt: The problem is that your%TIME% use a comma to separate the centiseconds, instead of a point! Just add a comma inDELIMS=:.,"...CommentedJun 22, 2015 at 14:14
 |  Show9 more comments
How to Edit
  • Correct minor typos or mistakes
  • Clarify meaning without changing it
  • Add related resources or links
  • Always respect the author’s intent
  • Don’t use edits to reply to the author
How to Format
  • create code fences with backticks ` or tildes ~
    ```
    like so
    ```
  • add language identifier to highlight code
    ```python
    def function(foo):
        print(foo)
    ```
  • put returns between paragraphs
  • for linebreak add 2 spaces at end
  • _italic_ or**bold**
  • indent code by 4 spaces
  • backtick escapes`like _so_`
  • quote by placing > at start of line
  • to make links (use https whenever possible)
    <https://example.com>
    [example](https://example.com)
    <a href="https://example.com">example</a>
How to Tag

A tag is a keyword or label that categorizes your question with other, similar questions. Choose one or more (up to 5) tags that will help answerers to find and interpret your question.

  • complete the sentence:my question is about...
  • use tags that describe things or concepts that areessential, not incidental to your question
  • favor usingexisting popular tags
  • read the descriptions that appear below the tag

If your question is primarily about a topic for which you can't find a tag:

popular tags »


lang-py

[8]ページ先頭

©2009-2025 Movatter.jp