IF %SubString% IS IN %String% THEN DO Something
I hope to understand this before I go further, but the longer term
goal would be to also do something like this:
IF %SubString1% OR %SubString2% OR %SubString3% IS IN %String% THEN DO
Something
I've seen other places where it appears that people are piping the
string into a FIND command, but I can't seem to get this to work even
with very simple experiments. I would really like to avoid temp files
if possible, though of course if there is no other way I would live
with it.
Many thanks in advance.
>I am trying to construct a simple IF statement that would look like
>this in pseudocode:
>
>IF %SubString% IS IN %String% THEN DO Something
>
>I hope to understand this before I go further, but the longer term
>goal would be to also do something like this:
>
>IF %SubString1% OR %SubString2% OR %SubString3% IS IN %String% THEN DO
>Something
This is the simplest method I think.
@echo off
>file.tmp echo/%string%
set substring1=abc
set substring2=def
set substring3=hij
findstr "%substring1% %substring2% %substring3%" file.tmp>nul
if not errorlevel 1 echo found one of the strings
del file.tmp
pause
--
Regards,
Mic
The IF command does not have to ability to do this. Here is one way you can
use FIND which will return an exit code (ERRORLEVEL) which you can then use
IF to direct the program flow.
Using your subject as the string, we can search for substrings within it.
For this example, I will use ECHO to pipe the string into FIND.
@echo off
setlocal
set string=Boolean Test for Existence of Substring in String
set SubString1=test
echo %string% |find "%SubString1%" > nul
if errorlevel 1 goto notfound
echo "%SubString1%" is found in "%string%".
goto :eof
:notfound
echo "%SubString1%" not found in "%string%".
Note, this will return not found because the comparison is case sensitive.
If you change SubString1 to match the case found in string, then it will be
found. Likewise, To make it case insensitive, simply add the /i switch to
FIND and the string will also be found.
**** OR condition testing ****
Here is a way to test multiple conditions and continue based on the first
one found. This one includes the /i switch for all three FIND commands.
@echo off
setlocal
set string=Boolean Test for Existence of Substring in String
set SubString1=test
set SubString2=sub
set SubString3=exist
echo %string% |find /i "%SubString1%" > nul
if not errorlevel 1 goto found
echo %string% |find /i "%SubString2%" > nul
if not errorlevel 1 goto found
echo %string% |find /i "%SubString3%" > nul
if not errorlevel 1 goto found
echo Neither "%SubString1%", "%SubString2%" or "%SubString3%" were found
echo within "%string%".
goto :eof
:found
echo At least one SubString was found within "%string%".
--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)
> IF %SubString1% OR %SubString2% OR %SubString3% IS IN %String% THEN DO
> Something
>
> I've seen other places where it appears that people are piping the
> string into a FIND command, but I can't seem to get this to work even
> with very simple experiments. I would really like to avoid temp files
> if possible, though of course if there is no other way I would live
> with it.
>
You don't need temp files.
@Echo off&Setlocal EnableDelayedExpansion
set substr1=abc
set substr2=def
set substr3=hij
set "string=This is just a string conataining abc and hij"
Echo ___Test one substr_________________________________________
IF "!string:%substr1%=!" NEQ "%string%" Echo string conatains substr1
Echo ___Test three substrings and tell which____________________
set flag=0
IF "!string:%substr1%=!" NEQ "%string%" Set /A flag+=100
IF "!string:%substr2%=!" NEQ "%string%" Set /A flag+=20
IF "!string:%substr4%=!" NEQ "%string%" Set /A flag+=3
If flag GTR 0 Echo string contains substrings %flag:0=%
Echo ___Use pipe to findstr to check______________________________
echo %string%|findstr "%substr1% %substr2% %substr3%" >NUL ^
&&Echo string contains substr||Echo string doens't contain substr
HTH
--
Regards
Matthias
@echo off
set string=<your string>
set substring=<your substring>
call :search string %substring%
pause>nul
:search
call set tmp=%%%1:%2=%%
if %tmp%==%string% (echo "%2" was not found in %1) else (echo "%2" was
found in %1)
goto:eof
HOOOOO, a veratible cornucopia of alternative methods! Saved those to a new
folder under my INbox! Thanks much from a still-learninger<g>.
HTH,
Twayne`