dancingbush
dancingbush

Reputation: 2281

User input on a batch file crashes

I am trying to get user input from command line when running a bat file but it crashes when user enters input and presses return:

echo Welcome to Xyplex Log On

pause

set /p whichXplex = Why Xyplex server - 1 or 2 ?

if %whichXyplex% == 1(

REM COnnecting to xyplex one IP:Socket

echo Connecting to Xpyplex %whichXyplex%...



start telnet.exe 192.120.187.35 2000

)ELSE(

echo Conencting to xyplex %whichXyplex%....

start telnet 193.120.187.245 2000

)

REM Run the script
cssript logInXyplex.vbs

I am using Windows 7.

Any input appreciated.

Upvotes: 0

Views: 92

Answers (1)

JosefZ
JosefZ

Reputation: 30113

At first sight:

rem          y letter missing: variable name %whichXplex% versus %whichXyplex%
set /p whichXyplex= Why Xyplex server - 1 or 2 ?
rem harmful extra ^ space in front of =
rem         would create %whichXplex % variable instead of %whichXyplex%

rem missing space         v here
if  "%whichXyplex%" == "1" (
rem ^             ^    ^ ^ missing double quotes
  REM COnnecting to xyplex one IP:Socket
  echo Connecting to Xpyplex %whichXyplex%...
  start telnet.exe 192.120.187.35 2000

) ELSE (
rem   ^ missing spaces in )ELSE(
  echo Conencting to xyplex %whichXyplex%....
  start telnet 193.120.187.245 2000
)

Moreover, I would prefer

  • either choice command for single character input. choice.exe allows single key-presses to be captured from the keyboard (no extra Enter):
  • or assign default value to prevent empty (undefined) variable value in case if a user presses only Enter:

(and note consistently used double quotes in the set "varname=varvalue" syntax pattern)

choice code snippet:

choice /C:12 /M:"Which Xyplex server "
set "whichXyplex=%errorlevel%"

set /P code snippet:

set "whichXyplex=1"
set /p "whichXyplex= Which Xyplex server - 1 or 2 (default=%whichXyplex%)?"

Upvotes: 2

Related Questions