spaces in string comparison

  • Thread starter Thread starter djc
  • Start date Start date
D

djc

IF %testvar%==yes echo yes

above works fine. If testvar is yes it echos yes. If not. nothing.

SET testvar=yes sir
IF %testvar%=="yes sir" echo yes sir

above does not work. I get: sir=="yes sir" was unexpected at this time.

SET testvar=yes sir
IF %testvar%==yes sir echo yes sir

above gives me: sir==yes was unexpected at this time.

How can I get this comparison to work? I know its the spaces because the
same construct works fine using values with no spaces.

any help is appreciated. Thanks.
 
djc said:
IF %testvar%==yes echo yes

above works fine. If testvar is yes it echos yes. If not. nothing.

But it fails with an error message if testvar is not defined or defined as a
string containing blanks. Therefore not a good syntax to use.
SET testvar=yes sir
IF %testvar%=="yes sir" echo yes sir

above does not work. I get: sir=="yes sir" was unexpected at this time.

I get a different result. Are you sure that you used the double-equals sign
"==" in your test, or did you perhaps inadvertently use the single-equals
like I just did trying a few things out?

Logically it could not possibly work, unless the contents of testvar
happened to include the surrounding doublequotes.
SET testvar=yes sir
IF %testvar%==yes sir echo yes sir

above gives me: sir==yes was unexpected at this time.

Oddly enough, that is the error message I would expect. When you type the
command and I read it, we both *know* that "echo" is the command to be
executed because it is the first word following the "==" that we recognize
as a common command. But what if I had an executable called yes.exe, and I
wanted this command:

sir echo yes sir

to be executed if the testvar variable was set to the value of "yes"? The
only possible answer would be that I would use this command:

IF %testvar%==yes sir echo yes sir

Since that command is identical to the one that you proposed that was to
simply echo "yes sir", isn't it clear that the command is not giving cmd.exe
enough information?
How can I get this comparison to work? I know its the spaces because the
same construct works fine using values with no spaces.

There is no absolutely foolproof way to ensure that all such comparisons can
be made fully foolproof, given various special characters and so on. But
assuming the strings being compared contain only text and whitespace
characters, the most reliable method is to double-quote both sides:

if "%testvar%"=="yes sir" echo yes sir

I prefer the other form, though:

if "%testvar%" EQU "yes sir" echo yes sir

/Al
 
Al Dunbar said:
But it fails with an error message if testvar is not defined or defined as a
string containing blanks. Therefore not a good syntax to use.

Good point. I am explicitly setting the variable to a default value at the
beginning of the script to avoid that scenario. All my examples for this
post are just silly snippets to illustrate where I was coming from.
I get a different result. Are you sure that you used the double-equals sign
"==" in your test, or did you perhaps inadvertently use the single-equals
like I just did trying a few things out?

I'm sure.
Logically it could not possibly work, unless the contents of testvar
happened to include the surrounding doublequotes.


Oddly enough, that is the error message I would expect. When you type the
command and I read it, we both *know* that "echo" is the command to be
executed because it is the first word following the "==" that we recognize
as a common command. But what if I had an executable called yes.exe, and I
wanted this command:

sir echo yes sir

to be executed if the testvar variable was set to the value of "yes"? The
only possible answer would be that I would use this command:

IF %testvar%==yes sir echo yes sir

Since that command is identical to the one that you proposed that was to
simply echo "yes sir", isn't it clear that the command is not giving cmd.exe
enough information?

Yep. I was clear that was the problem. I just didn't know how to fix it.
There is no absolutely foolproof way to ensure that all such comparisons can
be made fully foolproof, given various special characters and so on. But
assuming the strings being compared contain only text and whitespace
characters, the most reliable method is to double-quote both sides:

if "%testvar%"=="yes sir" echo yes sir

I prefer the other form, though:

if "%testvar%" EQU "yes sir" echo yes sir

/Al
And finally,
I must have typo'd! What you have suggested is the first way I did it but
must have made a typo. Then, after what I assumed was the correct way didn't
work, I was lost. Whoops. Its working good now.

Thanks for the help Al. : )
 
djc said:
IF %testvar%==yes echo yes

above works fine. If testvar is yes it echos yes. If not. nothing.

SET testvar=yes sir
IF %testvar%=="yes sir" echo yes sir

above does not work. I get: sir=="yes sir" was unexpected at this time.

Try:

if "%testvar%"=="yes sir" echo %testvar% == yes sir
 
Back
Top