search substring in string

  • Thread starter Thread starter thinktwice
  • Start date Start date
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?
 
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.
 
Back
Top