Putting those batch commands to sleep…

Funny … just when you think the whole world knows about some thing, you find out that ain’t always the case.


Someone asked recently how to create a delay between executing two separate commands in a batch file. The answer (of course)  is to use the ‘sleep.exe’ utility. But in Microsoft’s infinite wisdom, they don’t just give it to you … you’ve got to download it, and install it. And then remember to install it on every workstation that may need torun the batch script you’re creating.


Sleep.exe is included in the free Windows 2003 Resource Kit, which you can download from here:
http://www.microsoft.com/downloads/details.aspx?familyid=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en


And here is the command format:


C:\sleep /?
Usage:  sleep      time-to-sleep-in-seconds
        sleep [-m] time-to-sleep-in-milliseconds
        sleep [-c] commited-memory ratio (1%-100%)


So, to force a delay of 3 seconds, you would enter sleep 3 or sleep -m 3000

4 thoughts on “Putting those batch commands to sleep…

  1. Personally I put the ‘Additional’ exe files and com files form the resource kit in a sub folder of SYSVOL rather than on each workstation.

    At the start of each batch file map a drive to the sysvol folder as some commands still don’t work using UNC paths

  2. The SLEEP thing is based on guesswork and therefore unreliable as you don’t always now how long the batch file will take to complete.
    These things can vary.
    What Windows needs is a scheduling system that can do stuff like this :

    Say you have a programme that sends stats to an outside site every night, but you dont want it to send it until the batch that creates the stats has completed.

    !COMMENT Create nightly stats for Company A
    !RUNBATCH DoCompanyA.STATS.bat
    !PAUSE BATCH;WHILE LASTBATCH;EXISTS
    !
    !COMMENT Send nightly stats to Company A sites
    !RUNBATCH SendCompanyA.STATS.bat
    !PAUSE BATCH;WHILE LASTBATCH;EXISTS

    This is what can be done on the HP3000. (A HP system that died the death.) I modified the command names for clarity.
    You have one .bat file that includes a whole list of other .bat files and each one is performed when – and not before – the preceding .bat is finished using the PAUSE command. Very handy.

    I’m sure this could be done in Windows-land.

    PPP

  3. Doubt this'll ever get seen, but what the heck, I'm bored.

    stats.bat

    ============

    @echo off

    create.bat

    :createworking

    if exist locked.lck goto createworking

    send.exe

    ============

    create.bat

    ============

    @echo off

    ren unlocked.lck locked.lck

    rem insert whatever needs doing to create the stats here

    ren locked.lck unlocked.lck

    ============

  4. @PPP’s statement.

    You could use the START /WAIT, or the CALL function as these will both make the calling batch file wait for the called batch to complete. My understanding is that CALL should only be used for other batch files but, it has worked for other executables in my experience. And the START function actuall starts the process in a new window, so I generally use the START /MIN so as to start the new window minimized.

    call /?
    Calls one batch program from another.
    CALL [drive:][path]filename [batch-parameters]

    START /?
    Starts a separate window to run a specified program or command.

    START [“title”] [/Dpath] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
    [/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
    [/WAIT] [/B] [command/program]
    [parameters]

    -Lorentzen

Leave a Reply

Your email address will not be published. Required fields are marked *