Give me a break!
May 17, 2008 5:03 AM   Subscribe

How can I install/use the DOS batch scripting SLEEP command with Vista Ultimate? It does not exist by default. Back while scripting on XP, I used to install the free package "Windows Server 2003 Resource Kit Tools" which came with this command.

So basically, I just want to put a 60 second pause in a loop in a script. I am not looking for CPU intensive work-arounds or solutions involving cygwin.

Thanks.
posted by Slenny to Technology (5 answers total)
 
Compile this, call it "sleep", and call it from your scripts. Note that it uses strol to convert a number from a string to an integral type; care was taken to make sure it would not interpret octal- or hexadecimal-looking sequences as octal or hexidecimal. You can change that by changing the constant BASE to be zero. Also, care was taken to allow arguments of zero to be coreect (resulting in a zero sleep time), but negative sleep times to cause an error to be display (and returned).

"I am not looking for CPU intensive work-arounds or solutions involving cygwin."

You'll find it most convenient to compile this using cygwin. Compile it with a static library (oh lord is it big, that's C++ IOStreams for you, port it to C if you need it smaller) and you can use it on non-cygwin equipped machines.

For sixty seconds of sleep, call it like this:
sleep 60000
#include <iostream>
#include <cstdlib>
#include <windows.h>


int badargs(const char* name, const char* arg) {
    std::cerr << "Usage " << name << " unsigned_integral_milliseconds" << std::endl;
    if(arg) {
        std::cerr << "Argument \"" << arg << "\" is not an unsigned integral value" << std::endl;
    }
    return 1;
}

int main(int argc, char** argv) {
    if(argc != 2) {
        return badargs(argv[0], 0);
    }

    const int BASE = 10;
    char* p = 0 ;
    char* s = argv[1];
    
    long int sleepMillis = strtol(s, &p, BASE); 
    if(p == s || sleepMillis < 0 ) { // not unsigned number
        return badargs(argv[0], s);
    }
    // std::cout << "Sleeping for " << sleepMillis << " milliseconds" << std::endl; 
    Sleep(sleepMillis);
    return 0;
}

posted by orthogonality at 6:23 AM on May 17, 2008


Here's the same in C, for qa much smaller executable:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>


int badargs(const char* name, const char* arg) {
    fprintf( stderr, "Usage %s unsigned_integral_milliseconds\n", name);
    if(arg) {
        fprintf( stderr, "Argument \"%s\" is not an unsigned integral value\n", arg) ;
    }
    return 1;
}

int main(int argc, char** argv) {
    if(argc != 2) {
        return badargs(argv[0], 0);
    }

    const int BASE = 10;
    char* p = 0 ;
    char* s = argv[1];
    
    long int sleepMillis = strtol(s, &p, BASE); 
    if(p == s || sleepMillis < 0 ) { // not unsigned number
        return badargs(argv[0], s);
    }
    /* printf( "Sleeping for %li milliseconds.\n", sleepMillis); */
    Sleep(sleepMillis);
    return 0;
}

posted by orthogonality at 6:30 AM on May 17, 2008 [1 favorite]


You can ping yourself X amount of times to create a delay..

ping -n 5 localhost
posted by mattdini at 8:51 AM on May 17, 2008 [1 favorite]


Best answer: What about Vista's timeout command?
posted by I EAT TAPAS at 10:25 AM on May 17, 2008


Response by poster: timeout is simple and works great.

Thanks people!
posted by Slenny at 11:13 AM on May 17, 2008


« Older Help me Become an IT Ninja!   |   Book 'em Danno Newer »
This thread is closed to new comments.