JimmyCode
JimmyCode

Reputation: 57

Batch If/Else syntax error

I've written a batch file to help organize a few things, and I've run into multiple syntax errors with my if/else statements on the first page.

Code:

:Home
cls
title Home
color 0a



echo.

echo  --------------------
echo ^|                    ^|
echo ^|  A) Mon, Wed, Fri  ^|
echo ^|                    ^|
echo ^|                    ^|
echo ^|  B) Tue, Thur      ^|
echo ^|                    ^|
echo ^|                    ^|
echo  --------------------

set /p d = Please select a day set.

IF %d%==A (
    goto Mon
)
IF %d%==a (
    goto Mon
)
IF %d% == B (
    goto Tue
)
IF %d% == b (
    goto Tue
) ELSE (
    echo Invalid Input. Please try again
    set /a sum = x+1 
    set x == sum
    goto Home
)

:Mon

The problem that I've run into is right after creatng the variable 'd'. I get the error "( was unexpected at this time." I've tried moving the parentheses to fix it, going from upper to lower case, moving my conditions of the if statements onto the same line as the statement, and removing the parentheses. I'm assuming it's a simple syntax error but my code looks the same as all the code I looked at trying to solve this issue on my own. Thank you.

Upvotes: 0

Views: 623

Answers (1)

SomethingDark
SomethingDark

Reputation: 14305

This is the classic "set-includes-spaces-in-the-variable-name" problem.

Because of how you formatted your code, you've actually created a variable called %d %. You've also set the value of that variable to Please select a day set. (there's a space at the very beginning).

Remove the spaces from both sides of the equals sign and you'll be good to go.

set /p d=Please select a day set.

Upvotes: 3

Related Questions