Checking For Strings Within Strings

I need a piece of code (might be powershell) that checks for strings within strings. I want to place that code inside a batch file.
eg.
set A=abc
set B= ". . . something here that may contain special characters like ?/%. . ."
the line I'm looking for: C is 0 if B contains A if not C is 1
back to cmd file: if C is 0 do this if C is 1 do that
axcxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
Here's a pure batch approach, let me know if you have questions.

@echo off
setlocal EnableDelayedExpansion

set "A=abc"
set "B=something here that may contain special characters like ?/%%"
set "BB=!B:%A%=!"
if "%BB%" EQU "%B%" (
  echo Not found
  REM Place logic for substring not found here...
) else (
  echo Found
  REM Place logic for substring ound here...
)

Open in new window

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
footechCommented:
Here's a PowerShell approach.
$a = 'abc'
$b = '. . . something here that may contain special characters like ?/%. . .'
$c = 1
If ($b -match $a)
{ $c = 0 }
If ( $c -eq 0 )
{ #something }
ElseIf ($c -eq 1 )
{ #something else }

Open in new window

0
Dan CraciunIT ConsultantCommented:
@footech: to make sure the script works even when $a contains special characters (*, ?, etc), I would add this:
$a = 'abc?'               # use whatever string you want here
$a = [regex]::Escape($a)  # escape any special characters, so the string can be used correctly in a match.

Open in new window

HTH,
Dan
0
Hey MSSPs! What's your total cost of ownership?

WEBINAR: Managed security service providers often deploy & manage products from a variety of solution vendors. But is this really the best approach when it comes to saving time AND money? Join us on Aug. 15th to learn how you can improve your total cost of ownership today!

footechCommented:
@Dan - You are correct.  I didn't consider that scenario as it wasn't mentioned in the question.
0
axcxAuthor Commented:
Bill, it always displays FOUND. And if you try echo %B% there's an error 0x2345.
0
axcxAuthor Commented:
Footech, how can I place your piece of code inside a batch file? Just like that or does it require "prefix"?
0
Bill PrewCommented:
Bill, it always displays FOUND. And if you try echo %B% there's an error 0x2345.
Can you post the modified script you are running that is failing please.
0
axcxAuthor Commented:
It works great if B is static. But in my script B is taken from the clipboard using Powershell.

cls
@echo off
setlocal EnableDelayedExpansion
set "A=abc"
for /F "tokens=*" %%B in ('PowerShell -NoProfile -STA -Command "& { Add-Type -Assembly PresentationCore;   [Windows.Clipboard]::GetText() }"') do set a=%%B
set "BB=!B:%A%=!"
if "%BB%" EQU "%B%" (
  echo Not found
  REM Place logic for substring not found here...
) else (
  echo Found
  REM Place logic for substring ound here...
)

Open in new window

0
axcxAuthor Commented:
I see it myself now . . . do set a=%%B . . . it should be do set b=%%B. It seems ok now.
0
Bill PrewCommented:
I think you wanted to assign B to the result of the powershell fetch of the clipboard, not A.

This works for me (I added in a couple of echo's of the A and B values).

@echo off
setlocal EnableDelayedExpansion

set "A=abc"
for /F "tokens=*" %%B in ('PowerShell -NoProfile -STA -Command "& { Add-Type -Assembly PresentationCore;   [Windows.Clipboard]::GetText() }"') do set B=%%B
echo A:[%A%]
echo B:[%B%]
set "BB=!B:%A%=!"
if "%BB%" EQU "%B%" (
  echo Not found
  REM Place logic for substring not found here...
) else (
  echo Found
  REM Place logic for substring ound here...
)

Open in new window

~bp
0
footechCommented:
I'm really not sure how you would integrate what I posted into a batch file as that's not something I've ever had to do.  But it would probably depend on what you're trying to do in the batch file, like just echo the result, or use the result for further processing.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.