agent provocateur
agent provocateur

Reputation: 830

Batch String Comparison with Symbols

How do I do a string comparison with a string containing symbols? In my particular case, I am trying to compare a string to "%1" (including quotations).

ex:

if "%var%" neq '"%1"' ( echo %var% )

I have tried various forms, my latest looks like:

if "%var%" neq """%%1"""

Upvotes: 2

Views: 349

Answers (2)

woxxom
woxxom

Reputation: 73586

Replace the quotes in the variable with something else while comparing. And in case var already has that symbol in those places do the comparison with two different characters to make this trick universal:

if "%var:"=*%%var:"=#%" neq "*%%1*#%%1#" echo Yay!

Upvotes: 2

SomethingDark
SomethingDark

Reputation: 14305

In order to prevent batch from think you're talking about the %1 script argument, you need to double up the percent signs. It also doesn't hurt to not use quotes around the two sides of the == operator, but you don't have to.

@echo off

cls
set /p var=

if [%var%]==["%%1"] echo yes
pause

If you want to use quotes, you can.

if "%var%"==""%%1"" echo yes

Upvotes: 0

Related Questions