Verify a string exists within another string?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
djangofan
Posts: 23
Joined: 04 Nov 2011 12:28

Verify a string exists within another string?

#1 Post by djangofan » 10 Jan 2012 11:41

If I have a string , containing all of my concatenated args to my batch script, that looks like this:

"-debug -verbose -normi -homedir -repo"

Is there a way to verify that "-verbose" exists within that string without using a space-delimmetted FOR loop to compare each token? Basically, I want a function that works like so IF it is at all possible :

Code: Select all

@ECHO off
CALL :MYFUNCTION "-verbose"
IF %ERRORLEVEL%==0 ECHO Success^^!
pause
:MYFUNCTION
[do something here]
EXIT /B %ERRORLEVEL%


So, in other words, I suppose I am looking for something like FINDSTR.exe that works on a string rather than on a file.
Last edited by djangofan on 10 Jan 2012 11:49, edited 1 time in total.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Verify a string exists within another string?

#2 Post by dbenham » 10 Jan 2012 11:48

You can use string search and replace to do a case insensitive search. Replace your search term with nothing, and see if the result matches the original:

Code: Select all

set "str=-debug -verbose -normi -homedir -repo"
if "%str:-debug=%" neq "%str%" (echo -verbose found) else (echo -verbose not found)


Dave Benham

djangofan
Posts: 23
Joined: 04 Nov 2011 12:28

Re: Verify a string exists within another string?

#3 Post by djangofan » 10 Jan 2012 11:51

Thank you thank you thank you! I was looking for an idea like that one. ;-)

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Verify a string exists within another string?

#4 Post by Squashman » 10 Jan 2012 12:17

Love Dave's code with the String replacement.

But yes in theory you could use Findstr and look at the errorlevel.

Code: Select all

E:\batch files>set "str=-debug -verbose -normi -homedir -repo"
E:\batch files>echo %str% | findstr /c:"-verbose"
-debug -verbose -normi -homedir -repo

E:\batch files>echo %errorlevel%
0

E:\batch files>echo %str% | findstr /c:"-verboses"

E:\batch files>echo %errorlevel%
1

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Verify a string exists within another string?

#5 Post by dbenham » 10 Jan 2012 12:22

The FINDSTR search is case sensitive by default.

Since you probably don't want to see the FINDSTR output, you might want to redirect the output to nul:

Code: Select all

echo %str%|findstr /c:"-verbose" >nul


Dave Benham

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Verify a string exists within another string?

#6 Post by Squashman » 10 Jan 2012 12:50

dbenham wrote:The FINDSTR search is case sensitive by default.

Since you probably don't want to see the FINDSTR output, you might want to redirect the output to nul:

Code: Select all

echo %str%|findstr /c:"-verbose" >nul


Dave Benham

That is one of things I like about this forum. The attention to detail everyone has with code.
Thanks Dave for pointing that out. :D

djangofan
Posts: 23
Joined: 04 Nov 2011 12:28

Re: Verify a string exists within another string?

#7 Post by djangofan » 10 Jan 2012 16:50

Thanks Squashman and dbenham . I wouldn't have imagined 2 different ways of doing it. Thank you!

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

Re: Verify a string exists within another string?

#8 Post by Aacini » 11 Jan 2012 00:13

You may also use my %SET/A% macro with TEST(strConst IN strVar) function this way:

Code: Select all

set argv=-debug -verbose -normi -homedir -repo
%set/a% pos=TEST("-verbose" IN argv)
if %pos% gtr 0 (echo -verbose found at position %pos%) else echo -verbose not found
Or you may directly use TEST function with no %SET/A% macro:

Code: Select all

call TEST "-verbose" IN argv pos=
if %pos% gtr 0 (echo -verbose found at position %pos%) else echo -verbose not found
Both SET/A macro and TEST function are described in this topic.

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Verify a string exists within another string?

#9 Post by alan_b » 11 Jan 2012 12:15

dbenham wrote:You can use string search and replace to do a case insensitive search. Replace your search term with nothing, and see if the result matches the original:

Code: Select all

set "str=-debug -verbose -normi -homedir -repo"
if "%str:-debug=%" neq "%str%" (echo -verbose found) else (echo -verbose not found)


Dave Benham

I like it, quite a lot.
I have two immediate uses for it,
but for one use it has a killer bug that I had to fix.
It MIGHT be relevant to this topic also.

I have two specific command words, e.g. "DoThis" and "DoThat".
I have a file with 8,000 lines,
and I need to reject each line which starts with some other word,
e.g. words such as "dothis DOTHAT DoNowt Dot that"

In the following my first attempt was Demo1,
which worked correctly for the first three words,
but the last two illegal words were partial matches for legal words and were wrongly validated.

My second attempt Demo2 uses # as boundary markers for the legal words, and all the results are good.
You can of course use any boundary markers that do not appear within the legal words.

Code: Select all

@echo off & setlocal EnableDelayedExpansion
call :demo1 "DoThis DoThat" "dothis DOTHAT DoNowt Dot that"
call :demo2 "#DoThis# #DoThat#" "dothis DOTHAT DoNowt Dot that"
:pause
exit/b

:Demo1
set str=%~1
echo( & echo( validating random {%~2} against legal {%~1}
for %%a in (%~2) do (
  if "!str:%%a=!"=="%str%" (echo rejected   %%a) else (echo validated  %%a )
)
exit /b

:Demo2
set str=%~1
echo( & echo( validating random {%~2} against legal {%~1}
for %%a in (%~2) do (
  if "!str:#%%a#=!"=="%str%" (echo rejected   %%a) else (echo validated  %%a )
)
exit /b


Code results :-

Code: Select all

E:\T\CCleaner\v313\WinApp_2>x

 validating random {dothis DOTHAT DoNowt Dot that} against legal {DoThis DoThat}
validated  dothis
validated  DOTHAT
rejected   DoNowt
validated  Dot
validated  that

 validating random {dothis DOTHAT DoNowt Dot that} against legal {#DoThis# #DoThat#}
validated  dothis
validated  DOTHAT
rejected   DoNowt
rejected   Dot
rejected   that

E:\T\CCleaner\v313\WinApp_2>


Regards
Alan

djangofan
Posts: 23
Joined: 04 Nov 2011 12:28

Re: Verify a string exists within another string?

#10 Post by djangofan » 12 Jan 2012 10:53

@Aacini - just when I thought I had mastered batch scripts, the rabbit hole gets even deeper. Thanks for the reference to Macros.

Post Reply