5

I have a batch script where I need to set multiple variables when a user enters a number. For some reason the country isn't getting set. What am I doing wrong?

set /P CLIENTOPTION=CLIENT:
IF /I '%CLIENTOPTION%'=='1' set CLIENTCHOICE=y set COUNTRY=UK
IF /I '%CLIENTOPTION%'=='2' set CLIENTCHOICE=w set COUNTRY=UK
IF /I '%CLIENTOPTION%'=='3' set CLIENTCHOICE=x set COUNTRY=UK
IF /I '%CLIENTOPTION%'=='4' set CLIENTCHOICE=y set COUNTRY=USA
IF /I '%CLIENTOPTION%'=='5' set CLIENTCHOICE=z set COUNTRY=CANADA

2 Answers 2

8

This could be made more robust but all you are missing is the & command separator.

set /P CLIENTOPTION=CLIENT:
IF /I '%CLIENTOPTION%'=='1' set CLIENTCHOICE=y&set COUNTRY=UK
IF /I '%CLIENTOPTION%'=='2' set CLIENTCHOICE=w&set COUNTRY=UK
IF /I '%CLIENTOPTION%'=='3' set CLIENTCHOICE=x&set COUNTRY=UK
IF /I '%CLIENTOPTION%'=='4' set CLIENTCHOICE=y&set COUNTRY=USA
IF /I '%CLIENTOPTION%'=='5' set CLIENTCHOICE=z&set COUNTRY=CANADA
Sign up to request clarification or add additional context in comments.

Comments

3

The assignment uses everything on the same line after =. For example, for CLIENTOPTIONS === 1, the variable CLIENTCHOICE is being assigned everything after =, which is the string "y set COUNTRY=UK". Obviously, not what you intended.

You can get around this, and get the expected behavior, by using parentheses, and placing each action on a separate line, like so:

set /P CLIENTOPTION=CLIENT:
IF /I '%CLIENTOPTION%'=='1' ( 
    set CLIENTCHOICE=y
    set COUNTRY=UK
)
IF /I '%CLIENTOPTION%'=='2' ( 
    set CLIENTCHOICE=w 
    set COUNTRY=UK
)
IF /I '%CLIENTOPTION%'=='3' ( 
    set CLIENTCHOICE=x 
    set COUNTRY=UK
)
IF /I '%CLIENTOPTION%'=='4' ( 
    set CLIENTCHOICE=y 
    set COUNTRY=USA
)
IF /I '%CLIENTOPTION%'=='5' (
    set CLIENTCHOICE=z 
    set COUNTRY=CANADA
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.