how to use sleep or wait in a simple command execution

well, the obvious is to use “sleep.exe”. what if you don’t have it available? this is something i ran into today when i was on a server, needed to loop through a log and keep searching for a value… and didn’t want to sit around waiting.

simply, this was the command i was trying to get to run:

for /l %a in (1,1,5000) do find /i “failure” myLog.log & sleep 10

this didn’t work because sleep.exe wasn’t there. so, i did what any good internet citizen would do: google. i came up with this link: http://malektips.com/dos0017.html.

it gives a couple of examples using choice and ping. since choice was available, i decided to try this. my constructed command became:

for /l %a in (1,1,5000) do choice /t 10 /d y >nul & find /i “failure” myLog.log

now i get a 10 second delay between each loop. :)

Comments

  1. Hey Marcus, this is Troy...Good Tip!!

    I found that there is a two (2) percent-symbols '%%a' instead of one. Also, I understand how a For...In...Do works in a batch-file, however, what is the reason for the three elements and the values you chose, inside the parethesis (e.g. 1,1,5000)?

    Thanks

    ReplyDelete
  2. Troy again...

    It just dawned on me why your example only has one percent-symbol (e.g. %a), as opposed to having two (e.g. %%a). When running directly on the command-line, you only need one percent-symbol. When running within a batch-file, you need two.

    Details, details...

    ReplyDelete
  3. hey troy! it was good seeing you at smug and nice hearing from you.

    yes, you're absolutely right. %% is required when for loops are handled inside of a batch file. i was simply running that from the cmd shell, searching for a certain condition to pop up.

    regarding the other question about (1,1,5000), when you use FOR /L you're specifying to step through a sequence of numbers. what's inside the parentheses breaks down to the following:

    1 - first # in sequence
    1 - indicates the # of steps
    5000 - last # in sequence

    so i'm telling it, do this starting from 1 until i get to 5000, and go 1 number at a time. switching the second value to 5, for example would step 5 numbers at a time.

    hope that helps...

    ReplyDelete
  4. Thanks for the explanation, Marcus!! I never knew you could do that...

    ReplyDelete
  5. for very long i was trin the cmd scripts....wel to make users in server no problum but to run some condition and the bigest help is the ping sweep....thanks a lot dude..u have been a big help...keep up the good work...:)

    ReplyDelete

Post a Comment