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 type2 or type3


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?
 
/////mytest.txt///////////////////////////////
type1:AA
type1:BB
type2:CC
type2:DD
type3:EE
///////////////////////////////////////////////////

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

This works here.

@echo off
set "typepattern=type2,type3"

for /f "tokens=1,2* delims=:" %%a in (
mytest.txt) do set a=%%a&set b=%%b& call :next %typepattern%
goto :EOF

:next
if "%a%"=="%1" echo set "%b%=match"
shift
if not "%1"=="" goto :next
 
thanks foxdrive, i have tried your batch file, it works. but i'm note
quite understand how it works.

1. is set "typepattern=type2,type3" equal to set
typepattern=type2,type3 ?
2. how the "shift" command works here?
(i have read the help of shift , it tells me it'll Changes the
position of replaceable parameters in a batch file, but in your batch
file there's only one parameter "typepattern", how it works?)
 
thanks foxdrive, i have tried your batch file, it works. but i'm note
quite understand how it works.

1. is set "typepattern=type2,type3" equal to set
typepattern=type2,type3 ?
2. how the "shift" command works here?
(i have read the help of shift , it tells me it'll Changes the
position of replaceable parameters in a batch file, but in your batch
file there's only one parameter "typepattern", how it works?)

It is called with
call :next type2,type3
and a quirk of the command line is that when call is used (or directly from
the command line) a comma is treated as a delimiter - hence the call is
treated as

call :next type2 type3

Then when shift is used it selects each entry in turn and this allows for
more than 2 type entries in the typepattern variable.

The same quirk also treats other characters as a delimiter such as =
and this has been the case since the MSDOS days.
 
More or less. The double quotes are there to avoid incidental trailing white
space. I generally parenthesize the whole set command for this purpose, so
the following commands all do the same thing:

set typepattern=type2,type3
set "typepattern=type2,type3"
(set typepattern=type2,type3)

Both techniques can also be used to ensure that trailing whitespace is
preserved, so the following commands all do the same thing:

set typepattern=type2,type3
set "typepattern=type2,type3 "
(set typepattern=type2,type3 )

Because whitespace is only visible when surrounded by visible characters,
you'd have some difficulty in seeing the difference between these two
un-delimited commands:

set typepattern=type2,type3
set typepattern=type2,type3

/Al
 
Back
Top