Does variable contain the target? Why doesn't this test work?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Does variable contain the target? Why doesn't this test work?

#1 Post by Jer » 28 Aug 2017 20:30

I am submitting this code that gives inconsistent results. I want to test
whether or not a string contains a character.
I'll probably end up :oops: but wiser. Thanks for your assistance.
Jerry

Code: Select all

@echo off & setlocal
Set "string=ABDEFG"
Set "target1=B" & Set "target2=C"
Echo test1
If /I "%string:%target1%=%"=="%target1%" (
   Echo %target1% not found in %string%
) Else (
   Echo %target1% found in %string%
)

If /I "%string:%target2%=%"=="%target2%" (
   Echo %target2% not found in %string%
) Else (
   Echo %target2% found in %string%
)
endlocal

setlocal EnableDelayedExpansion
Set "string=ABDEFG"
Set "targ1=B" & Set "targ2=C"

Echo( & Echo test2
If /I "!string:!targ1!=!"=="!targ1!" (
   Echo !targ1! not found in !string!
) Else (
   Echo !targ1! found in !string!
)

If /I "!string:!targ2!=!"=="!targ2!" (
   Echo !targ2! not found in !string!
) Else (
   Echo !targ2! found in !string!
)

endlocal & exit /b


result:
test1
B found in ABDEFG
C found in ABDEFG

test2
B found in ABDEFG
C found in ABDEFG

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Does variable contain the target? Why doesn't this test work?

#2 Post by Aacini » 28 Aug 2017 20:53

Code: Select all

@echo off
setlocal EnableDelayedExpansion

Set "string=ABDEFG"
Set "targ1=B" & Set "targ2=C"

Echo( & Echo Right test
If /I "!string:%targ1%=!"=="%string%" (
   Echo %targ1% not found in %string%
) Else (
   Echo %targ1% found in %string%
)

If /I "!string:%targ2%=!"=="%string%" (
   Echo %targ2% not found in %string%
) Else (
   Echo %targ2% found in %string%
)


I suggest you to read the explanations given at this answer

Antonio

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: Does variable contain the target? Why doesn't this test work?

#3 Post by Jer » 28 Aug 2017 23:26

Thank you Aacini for the correction. My test should only have been with
setlocal EnableDelayedExpansion, and I was comparing the edited string with
the target instead of the un-edited string.

I read your answer in the link, a down-to-earth explanation of arrays and delayed expansion.
Anyone interested in learning about managing values with arrays in batch could benefit from it.

An excerpt from your answer that explains why delayed expansion is needed:
If you want to use another variable as index, you must know that the replacement of variables enclosed
in percent symbols by their values is parsed from left to right; this means that:

set i=2
echo %elem[%i%]%

doesn't give the desired result because it means: show the value of the elem[ variable, followed by i, followed by
the value of the ] variable.

Post Reply