Findstr error: "Search string too long": How to extract and match substring in "for" loop?

3083 views
Skip to first unread message

Robert Keenan

unread,
Jan 20, 2009, 4:17:30 PM1/20/09
to
In a MSDOS batch script under WinXP I coded (ignore autom.line wraps):

for /f "delims=" %%i in (%patternfile%) do (findstr /b /e /c:"%%i" "%targetfile%" >nul || echo\%%i) >>%newfile%

However this yields occasionally an error on the output console:

"Search string too long"

First question: What is the maximum length which FINDSTR can work with?

As far as I can imagine the first 50-60 chars of each line are significant
and unique. So in a second approach I could try to match only the lets say first 50 chars of each line.
But how to I extract them? WinXP offers some smart string operations but they do
not work in "for" loops. It must be somehow similar to (note the omitted /e):

setlocal enabledelayedexpansion
for /f "delims=" %%i in (%patternfile%) do
(set temp=%%%i:~50% & findstr /b /c:"!temp!" "%targetfile%" >nul || echo\%%i) >>%newfile%

But this does not work How do I have to write it else?

Rob

Tom Lavedas

unread,
Jan 20, 2009, 4:50:40 PM1/20/09
to

There are two ways ...

1: With delayed expansion


setlocal enabledelayedexpansion
for /f "delims=" %%i in (%patternfile%) do (

set temp=%%i
set temp=!temp:~0,50!


findstr /b /c:"!temp!" "%targetfile%" >nul || echo\%%i) >>%newfile%

2: With a subroutine
for /f "delims=" %%i in (%patternfile%) do call sub: "%%i"
goto :EOF

:sub
set temp=%~1
set temp=%temp:~0,50%
findstr /b /c:"%temp%" "%targetfile%" >nul || echo\%~1) >>%newfile%

In either case, you need to use an intermediate storage line to get
the argument into the variable before it is parsed. It can't be done
all at one time.

BTW, I have no idea what the limit on the search string is in FINDSTR.

Tom Lavedas
***********
http://there.is.no.more/tglbatch/

foxidrive

unread,
Jan 20, 2009, 7:31:44 PM1/20/09
to
On 20 Jan 2009 21:17:30 GMT, ro...@ozone.ca (Robert Keenan) wrote:

>However this yields occasionally an error on the output console:
>
>"Search string too long"
>
>First question: What is the maximum length which FINDSTR can work with?

It seems to be 127 characters.

@echo off
set string=11111111112222222222333333333344444444445555555555
set string=%string%%string%%string%%string%%string%%string%%string%
for /L %%a in (1,1,1000) do call :next %%a
goto :EOF
:next
echo %1
call set var=%%string:~0,%1%%
echo %var%|findstr /c:"%var%">nul
pause

billious

unread,
Jan 20, 2009, 8:33:33 PM1/20/09
to

"Robert Keenan" <ro...@ozone.ca> wrote in message
news:49763f6a$0$31875$9b4e...@newsspool3.arcor-online.net...

I'm not sure what the max length is - perhaps an experiment with a
parrten-file with contents

1
12
123
1234
(etc...)

would show you.

Since you appear to be looking for an exact line-match, then perhaps

for /f "delims=" %%i in (patternfile) do for /f "delims=" %%j in
(targetfile) do if %%i==%%j >>newfile echo\%%i

would work - but I'd anticipate it'd be slow if the files were large.

If you want to experiment with the substring option, then you need to
substring an environment variable, as you can't directly substring a
metavariable

set var=%%i
set var=!var:~start,length!
findstr...

The downside to this approach is that it's susceptible to "poison"
characters, whereas the if %%i==%%j approach appears to be immune as it
operates directly on the metavariables.


par...@gmail.com

unread,
Jan 22, 2009, 3:51:51 PM1/22/09
to

FINDSTR's limit is 127 characters.

Consider using GREP instead. It does not have such limits & will not
balk when encountering special characters like '{'.

for /f "delims=" %%i in (%patternfile%) do (GREP -Fxsq "%%i"
%targetfile% || echo\%%i) >>%newfile%

Note: %targetfile% should not have quotemarks when using GREP

Reply all
Reply to author
Forward
0 new messages