[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Checking For Strings Within Strings

Posted on 2014-08-31
11
Medium Priority
?
137 Views
Last Modified: 2014-09-01
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
0
Comment
Question by:axcx
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 59

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 40295879
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
 
LVL 41

Expert Comment

by:footech
ID: 40295900
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
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40296065
@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
New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

 
LVL 41

Expert Comment

by:footech
ID: 40296140
@Dan - You are correct.  I didn't consider that scenario as it wasn't mentioned in the question.
0
 

Author Comment

by:axcx
ID: 40296613
Bill, it always displays FOUND. And if you try echo %B% there's an error 0x2345.
0
 

Author Comment

by:axcx
ID: 40296615
Footech, how can I place your piece of code inside a batch file? Just like that or does it require "prefix"?
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 40297146
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
 

Author Comment

by:axcx
ID: 40297179
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
 

Author Comment

by:axcx
ID: 40297319
I see it myself now . . . do set a=%%B . . . it should be do set b=%%B. It seems ok now.
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 40297330
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
 
LVL 41

Expert Comment

by:footech
ID: 40297345
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Loops Section Overview

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question