Alex
Alex

Reputation: 71

Can't make batch string comparison work

I'm sorry that this question has been asked multiple times and I'm re-asking it, but the truth is I've tried every possible thing and it just doesn't want to work!

@echo off
echo This is a test batch file
echo What colour would you like?
set "colour="
set /p colour =
if "%colour%"==red color c
if "%colour%"==blue color b
PAUSE

I've tried with

set /p "colour"=

or

set /p "colour"="%colour%"

or

if "%colour%" == "red" color b

Please help! Thank you!

Upvotes: 0

Views: 42

Answers (1)

Magoo
Magoo

Reputation: 80213

set /p colour=
if "%colour%"=="red" color c

Batch is sensitive to spaces in a SET statement. SET FLAG = N sets a variable named "FLAGSpace" to a value of "SpaceN"

Since LHS of the comparison is "quoted", so must be the RHS for a match to appear

Tip: /i will make the match case-insensitive.

Upvotes: 1

Related Questions