We help IT Professionals succeed at work.

Choice from a batch file w/timeout

Please look that the attached, I had this running a long time ago on W2K server, but when we upgraded, it seems as if the syntax of the Choice command have changed.

I keep receiving errors on the syntax of the choice command.

thanks



:Top

@Echo off
::Cls

Set Host=%1

:: If no parameter was passed, then set the host to old system.
if (%1) == () Set Host="89.26.210.1" 

Ping %Host%

Echo.
choice /C:pabcdefghijklmnoqrstuvwxyz0123456789.- /N /T:N,15 /D:P /M "Type 'P' to ping the 

host again ... Anything else to Exit."

::Echo %errorlevel%

::pause

if %errorlevel% == 1 goto Top

Open in new window

Comment
Watch Question

Can you post the error?  If it's timeout, you should have just one number.

The help shows:
 /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.

Or when editing, the wrapping might have messed it up when you saved it.
Steven CarnahanAssistant Vice President\Network Manager

Commented:
Taken from:  http://malektips.com/dos0017.html

Windows XP and Windows 2003 machines

Since Windows XP and 2003 users do not have the CHOICE command, if you find the PING alternative not to your liking, the Windows 2003 Resource Kit has a batch file SLEEP command.

pony10us, That may be true, but I tried it on my Vista and Win7 boxes and they both had it.  Can you try it?  
Steven CarnahanAssistant Vice President\Network Manager

Commented:
I commented out the echo off to get the results and placed another pause at the end. I then ran it on my XP Pro and here is what I got:


C:\test>Set Host=

C:\test>if () == () Set Host="89.26.210.1"

C:\test>Ping "89.26.210.1"

Pinging 89.26.210.1 with 32 bytes of data:

Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 89.26.210.1:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

C:\test>Echo.


C:\test>choice /C:pabcdefghijklmnoqrstuvwxyz0123456789.- /N /T:N,15 /D:P /M "Typ
e 'P' to ping the
'choice' is not recognized as an internal or external command,
operable program or batch file.

C:\test>host again ... Anything else to Exit."
'host' is not recognized as an internal or external command,
operable program or batch file.

C:\test>if 9009 == 1 goto Top

C:\test>pause
Press any key to continue . . .
Doh, so it's really that "choice" command came in AFTER xp and 2003.  How about replacing it:

http://www.computerhope.com/sethlp.htm

Or you could work around it many other ways.

Author

Commented:
Here is the Error: ERROR: The timeout factor is invalid.

I thought the 'Choice' command was intrinsic to the Windows OS ?  I found a copy of it in the ...\System32 folder  and in the I386 folder.
C:\>ver
Microsoft Windows [Version 5.2.3790]

When I look a the properties of the file it shows 'Microsoft Corporation' and the file version is the same as the windows version.  See the code snip for the the help.

Thanks.




C:\>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."

Open in new window

So, you should have just one numbe for the /T option and no ":"

choice /C pabcdefghijklmnoqrstuvwxyz0123456789.- /N /T 15 /D P /M "Type 'P' to ping the host again ... Anything else to Exit."
And the "." and "-" are not valid so:

choice /C pabcdefghijklmnoqrstuvwxyz0123456789 /N /T 15 /D P /M "Type 'P' to ping the host again ... Anything else to Exit."

Author

Commented:
It was the .- that was giving me problems.
This line of code worked on the last version of windows - W2K.

THanks