Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string comparison in batch file

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?

like image 739
dushyantp Avatar asked Feb 19 '13 09:02

dushyantp


People also ask

How can I compare two string?

Using String. equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true. If any character does not match, then it returns false.

Can you use comparison operators on strings?

The comparison operators also work on strings. To see if two strings are equal you simply write a boolean expression using the equality operator.

What is string comparison method?

The compareTo() method This method compares two Strings lexicographically. This method returns  a negative integer if current String object lexicographically precedes the argument string.  a positive integer if current String object lexicographically follows the argument  true when the strings are equal.


1 Answers

Just put quotes around the Environment variable (as you have done) :
if "%DevEnvDir%" == "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\"
but it's the way you put 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 
like image 60
AjV Jsy Avatar answered Sep 28 '22 08:09

AjV Jsy