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
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/
>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
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.
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