search substring in string


T

thinktwice

/////mytest.txt///////////////////////////////
type1:AA
type1:BB
type2:CC
type2:DD
type3:EE
///////////////////////////////////////////////////

setlocal enabledelayedexpansion

set typepattern="type2,type3" <-----here i means i want to search
all the types that match type1 or type2

for /f "tokens=1,2* delims=:" %%i in ('findstr /c:TYPE= C:
\mytest.txt') do @(
set mytype=%%i


set tmptype=%typepattern%
:loop
for /f "tokens=1,2* delims=," %%u in ("!tmptype!") do (
if /i "%%u"=="!mytype!" (set %%j=match) else (set tmptype=%%v&goto
loop) )
)

i expect this batch file could achieve this:
set CC=match
set DD=match
set EE=match

but it doesn't work. anything wrong in my batch?
 
Ad

Advertisements

M

Matthias Tacke

thinktwice said:
/////mytest.txt///////////////////////////////
type1:AA
type1:BB
type2:CC
type2:DD
type3:EE
///////////////////////////////////////////////////

setlocal enabledelayedexpansion

set typepattern="type2,type3" <-----here i means i want to search
all the types that match type1 or type2

for /f "tokens=1,2* delims=:" %%i in ('findstr /c:TYPE= C:
\mytest.txt') do @(
set mytype=%%i


set tmptype=%typepattern%
:loop
for /f "tokens=1,2* delims=," %%u in ("!tmptype!") do (
if /i "%%u"=="!mytype!" (set %%j=match) else (set tmptype=%%v&goto
loop) )
)

i expect this batch file could achieve this:
set CC=match
set DD=match
set EE=match

but it doesn't work. anything wrong in my batch?
Yes, several things. (I'll explain if wanted)

findstr uses by default regex's and multiple or'ed search strings.
So the easiest solution is to use these defaults. A caret anchors the
search string at line begin, so:

@echo off
for /f "tokens=2* delims=:" %%A in (
'findstr.exe "^type2 ^type3" C:\mytest.txt'
) do echo set %%A=match

should do what you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top