Solved Compare two strings in Batch

December 16, 2010 at 07:27:37
Specs: Windows 7
if Test==Test (
echo in if condition.
-- do something --
)
) else (
echo in else condition.
-- do something --
)
The output it is giving me is
in if condition
in else condition.

not sure why this is happening..even though two strings are same..can anyone help please??

thanks in advance


See More: Compare two strings in Batch

Report •


✔ Best Answer
December 16, 2010 at 08:38:08
First of all, something that's bugging me. Sentences begin with a capital letter, and end with a single period.

.it is going into else condition, instead of if condition
Then it's failing the IF test. Keep ECHO ON, and see what it's comparing.

How To Ask Questions The Smart Way



#1
December 16, 2010 at 07:41:02
)
) else (

You're ending your IF block, closing a non-existent block of code, and adding an ELSE block. Strangely, CMD never thinks about saying something.

Get rid of the ) line before the ) else ( line.

How To Ask Questions The Smart Way


Report •

#2
December 16, 2010 at 07:44:47
Well, it would be easier to know what's wrong if you put the exact code so that it can be tested... however I think I can give you a few pointers. Normally, when comparing two string in variable you I enclose them in "..." or $$...$$, depending on the content of the string. Then you could use the /I switch of the IF, which will compare the strings in case insensitive mode.

That's all I can think at this moment, however if you include the actual code may be I could look it and test it to help you find the problem.


Report •

#3
December 16, 2010 at 07:52:19
Good call Razor2.3! I can't believe I didn't see it.

Report •

Related Solutions

#4
December 16, 2010 at 08:05:02
Sorry Razor,
i typed it wrong..this is what iam trying to do..
setLocal EnableDelayedExpansion


if "%EZFROOT%" == "" echo Environment variable EZFROOT is not set. & goto _ERR1
if "%IS_HOME%" == "" echo Environment variable IS_HOME is not set. & goto _ERR1

set DEFAULT_VERSION=Interstage Application Server Standard-J Edition V9.1.0

For /F "tokens=* delims=" %%A in ('CALL isprintvl') Do Set INSTALLED_VERSION=%%A
Echo Installed Interstage Version is: %INSTALLED_VERSION%
pause
Echo %INSTALLED_VERSION%
Echo %DEFAULT_VERSION%
pause

if "%INSTALLED_VERSION%"==%DEFAULT_VERSION% (
echo in if condition
) else (
echo in else condition
)

the output iam getting is :

Installed Interstage Version is: Interstage Application Server Standard-J Editio
n V9.1.0
Press any key to continue . . .
Interstage Application Server Standard-J Edition V9.1.0
Interstage Application Server Standard-J Edition V9.1.0
Press any key to continue . . .
in if condition
in else condition

.. this call to other program is giving me -- Interstage Application Server Standard-J Edition V9.1.0.
I expect it should go in if condition...


Report •

#5
December 16, 2010 at 08:22:33
My guess? Your IF statement is silently failing, so you're not really in the IF block. Try this:
if "%INSTALLED_VERSION%"=="%DEFAULT_VERSION%" (

How To Ask Questions The Smart Way


Report •

#6
December 16, 2010 at 08:29:53
Tried it ..it is going into else condition, instead of if condition..
and if i remving quotes on DEFAULT_VERSION varaible , then again it would go itno both if and else..

i don't see it is going into if alone..not sure why..

thanks,
tester999


Report •

#7
December 16, 2010 at 08:38:08
✔ Best Answer
First of all, something that's bugging me. Sentences begin with a capital letter, and end with a single period.

.it is going into else condition, instead of if condition
Then it's failing the IF test. Keep ECHO ON, and see what it's comparing.

How To Ask Questions The Smart Way


Report •

#8
December 16, 2010 at 08:42:59
i just wrote some some sample program..that one also giving same result..going into if and else..

@echo off
setLocal EnableDelayedExpansion
set DEFAULT_VERSION=Interstage Application Server Standard-J Edition V9.1.0
set INSTALLED_VERSION=Interstage Application Server Standard-J Edition V9.1.0
pause
echo %DEFAULT_VERSION%
echo %INSTALLED_VERSION%
pause
if "%INSTALLED_VERSION%"==%DEFAULT_VERSION% (
echo in if condition
) else (
echo in else condition
)

pause
exit/b 0


Report •

#9
December 16, 2010 at 08:52:48
sorry ..for the confusion..the following is working ..the prgoram iam calling is adding extra space..INSTALLED_VERSION="Interstage Application Server Standard-J Edition V9.1.0 ", there is a space in result string..

now ..
this is working fine..
if %INSTALLED_VERSION%==%DEFAULT_VERSION% (
echo in if condition
) else (
echo in else condition
)


Report •

#10
December 16, 2010 at 08:55:47
It happens to me sometimes. In this cases you can use the set into () to avoid spaces at the end Eg.

(SET VAR=Value)

It helps to avoid this problem...


Report •

#11
December 16, 2010 at 08:59:47
it did not appear on the console actually..when echo is on..as per Razor suggestion..i observed it...

so your suggestion is to use something like this?

For /F "tokens=* delims=" %%A in ('CALL isprintvl') Do Set Into INSTALLED_VERSION="%%A"

instead of For /F "tokens=* delims=" %%A in ('CALL isprintvl') Do Set INSTALLED_VERSION="%%A"????

thanks in advance..


Report •

#12
December 16, 2010 at 10:37:15
Well, not exactly.

What I'm talking about is to use the parenthesis to enclose the SET statement so that you can delimit the value to assign to the variable. It would be something like this:

For /F "tokens=* delims=" %%A in ('CALL isprintvl') Do (
(Set INSTALLED_VERSION=%%A)
)

It's not required but in some cases I use it to avoid those annoying spaces that sometimes get appended at the end of the variables content when you set the value without noticing a couple of spaces at the end of the line.

Of course, this will not work if the output of the command have the spaces.


Report •

#13
December 16, 2010 at 11:40:34
ya..i did not expect the existing program would return ..but anyway it is working fine now..thanks to you and Razor for helping to debug the program...

Report •

#14
June 7, 2011 at 03:05:19
following is the code please help me out in "if" condition while comparing with if condition there its not resolved

jps > e:\tt.txt

set /p var= < e:\tt.txt

rem echo %var%

pause
for /f "tokens=1-2 delims= " %%a in (e:\tt.txt) do (

echo word 1: %%a
echo word 2: %%b
set name = %%b

IF "%name%"=="%CheckRTI%" (echo "Program Running")

)


:finish
echo "success"


pause


exit


Report •

Ask Question