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

Skip to first unread message

Robert Keenan

Jan 20, 2009, 4:17:30 PM1/20/09
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?


Tom Lavedas

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

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

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


Jan 20, 2009, 7:31:44 PM1/20/09
On 20 Jan 2009 21:17:30 GMT, (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
echo %1
call set var=%%string:~0,%1%%
echo %var%|findstr /c:"%var%">nul


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

"Robert Keenan" <> wrote in message

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


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

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

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.

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

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
0 new messages