97

How do we compare strings which got space and special chars in batch file?

I am trying:

if %DevEnvDir% == "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\"(
echo VS2010
)

But it gives an error "Files was unexpected at this time."

I tried:

if "%DevEnvDir%" == "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\"(
echo VS2010
)

But it gives an error "The syntax of the command is incorrect."

Any ideas?

dushyantp
  • 4,398
  • 7
  • 37
  • 59
  • Judging by the linked answer, you could do `if [%DevEnvDir%] == ["C:\..."]`. Link: https://stackoverflow.com/a/4953226/2428861 – Mladen B. Aug 07 '19 at 09:11

4 Answers4

114

Just put quotes around the Environment variable (as you have already done):

if "%DevEnvDir%" == "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\"

The strings you are comparing are fine, the problem is the way you put the opening bracket without a space. That is confusing it.

Works for me...

C:\if "%gtk_basepath%" == "C:\Program Files\GtkSharp\2.12\" (echo yes)
yes
NickS1
  • 496
  • 1
  • 6
  • 19
AjV Jsy
  • 5,799
  • 4
  • 34
  • 30
  • 5
    thanks for the hint!! The mistake I did was that, there was no space between by "string-value" & (. Now works fine! thanks. – dushyantp Feb 19 '13 at 10:59
  • 8
    This seems to fail if the variable already contains quotes. For example: if "%1"=="" goto Help fails if the first argument to the batch file already contains double quotes around it. – MarioVilas Jun 06 '13 at 13:54
  • 1
    So what the usage of not equal? – terwxqian Jan 20 '21 at 03:05
  • 1
    if not "example" == "example" See https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/if – AjV Jsy Jan 20 '21 at 08:56
  • "bracket" - do you mean parentheses? This is a bracket: `[` – johny why Nov 23 '21 at 23:07
44

While @ajv-jsy's answer works most of the time, I had the same problem as @MarioVilas. If one of the strings to be compared contains a double quote ("), the variable expansion throws an error.

Example:

@echo off
SetLocal

set Lhs="
set Rhs="

if "%Lhs%" == "%Rhs%" echo Equal

Error:

echo was unexpected at this time.

Solution:

Enable delayed expansion and use ! instead of %.

@echo off
SetLocal EnableDelayedExpansion

set Lhs="
set Rhs="

if !Lhs! == !Rhs! echo Equal

:: Surrounding with double quotes also works but appears (is?) unnecessary.
if "!Lhs!" == "!Rhs!" echo Equal

I have not been able to break it so far using this technique. It works with empty strings and all the symbols I threw at it.

Test:

@echo off
SetLocal EnableDelayedExpansion

:: Test empty string
set Lhs=
set Rhs=
echo Lhs: !Lhs! & echo Rhs: !Rhs!
if !Lhs! == !Rhs! (echo Equal) else (echo Not Equal)
echo.

:: Test symbols
set Lhs= \ / : * ? " ' < > | %% ^^ ` ~ @ # $ [ ] & ( ) + - _ =
set Rhs= \ / : * ? " ' < > | %% ^^ ` ~ @ # $ [ ] & ( ) + - _ =
echo Lhs: !Lhs! & echo Rhs: !Rhs!
if !Lhs! == !Rhs! (echo Equal) else (echo Not Equal)
echo.
Community
  • 1
  • 1
skataben
  • 2,023
  • 1
  • 22
  • 25
  • try this >>> @echo off setlocal EnableDelayedExpansion set Lhs="hello" set Rhs="hello" if !Lhs! == "hello" (echo 1Equal) :: Surrounding with double quotes also works but appears (is?) unnecessary. if "!Lhs!" == "hello" echo Equal timeout 10 endlocal pause – Subham Tripathi Dec 09 '14 at 06:43
  • 4
    You can use `if /I` if you wish to do case insensitive string comparison – Carlos P Feb 11 '15 at 09:54
0

A roundabout solution use a subroutine

CALL :Comparator %var1% %var2% <- or the string you want to compare
IF %retVal%==1 (
   do stuff
) ELSE (
   do other things
)

GOTO :eof

:Comparator
  IF "%~1" == "%~2" (set retVal=1) ELSE (set retVal=0)
GOTO :eof

It won't work if there is double-quotes inside the strings though, but if you compare file paths, there shouldn't be. %~[1-9] the tilde '~' removes double quotes around the variable, and then you put new ones around them if the strings has spaces inside. The tilde trick only works with passed variable though, hence the subroutine.

Erik
  • 101
  • 5
-6

The solution is DO NOT USE SPACES!

IF "%DevEnvDir%"=="C:\" (
Kiquenet
  • 14,494
  • 35
  • 148
  • 243
  • 4
    This is wrong. You can use spaces, as long as you quote them. The problem was the *missing* space at the end `if "%env%"=="xyz"(` – jeb Nov 14 '16 at 08:54