Foo Infusion

A periodic infusion of foo from the world of a junior developer

Wednesday, February 18, 2009

Pausing in a Windows batch script

It seems like I've been creating a lot of batch scripts lately.

I recently wanted a script that would start a server process, wait for the server to finish starting, and then start a client process.

I used the START command to kick off the server and client processes, but then was unsure of how to proceed to accomplish a pause/sleep in the batch script while the server was starting. Searching the internet seemed to turn up less information than I wanted, and often the suggested solutions didn't work anyway.

I settled on using the CHOICE command which has the following help screen:

C:\Users\admin>choice /?

CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]

Description:
This tool allows users to select one item from a list of choices and returns the index of the selected choice.

Parameter List:
/C choices Specifies the list of choices to be created. Default list is "YN".

/N Hides the list of choices in the prompt. The message before the prompt is displayed and the choices are still enabled.

/CS Enables case-sensitive choices to be selected. By default, the utility is case-insensitive.

/T timeout The number of seconds to pause before a default choice is made. Acceptable values are from 0 to 9999. If 0 is specified, there will be no pause and the default choice is selected.

/D choice Specifies the default choice after nnnn seconds. Character must be in the set of choices specified by /C option and must also specify nnnn with /T.

/M text Specifies the message to be displayed before the prompt. If not specified, the utility displays only a prompt.

/? Displays this help message.

NOTE:
The ERRORLEVEL environment variable is set to the index of the key that was selected from the set of choices. The first choice listed returns a value of 1, the second a value of 2, and so on.
If the user presses a key that is not a valid choice, the tool sounds a warning beep. If tool detects an error condition, it returns an ERRORLEVEL value of 255. If the user presses CTRL+BREAK or CTRL+C, the tool returns an ERRORLEVEL value of 0. When you use ERRORLEVEL parameters in a batch program, list them in decreasing order.

Examples:
CHOICE /?
CHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."
CHOICE /T 10 /C ync /CS /D y
CHOICE /C ab /M "Select a for option 1 and b for option 2."
CHOICE /C ab /N /M "Select a for option 1 and b for option 2."


So to accomplish a pause in the batch script execution, I used a line that looks like this:
CHOICE /N /D Y /T

No message is needed.
/N - don't bother printing the choices
/D Y - the default response to choose, should time run out (it will) is Y
/T - specifies the timeout (time to wait for a response before picking the default) 
 Not complicated, but it's something that seemed to be lacking from the knowledge of the internet.