Having for loop reference findstr result, moving two lines d

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Having for loop reference findstr result, moving two lin

#16 Post by foxidrive » 23 Mar 2013 10:12

Ta. Yours is very quick with the same sample file too, on my machine.

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: Having for loop reference findstr result, moving two lin

#17 Post by mfm4aa » 23 Mar 2013 12:01

Mmm, the code bricks, if %search% found on line 2 and %updown%=-1 :(

Code: Select all

Pattern:  [mysearch] found at line 2 in file.txt
Target line below zero


So it needs a (hopefully :) ) last modification:

Code: Select all

@echo off &setlocal
set "fname=%~1"
if not exist "%fname%" echo File %fname% not found!&goto:eof
set "search=%~2"
if not defined search echo Search string missing!&goto:eof
set /a updown=%~3 2>nul|| (echo Up/down count missing!&goto:eof)

set "skip0="
set /a skipcount0=0
:findloop
set "count="
for /f "%skip0%tokens=1*delims=:" %%i in ('findstr /n "%search%" "%fname%"') do if not defined count set /a count=%%i
if defined count (echo Pattern:  [%search%] found at line %count% in %fname%) else echo No more [%search%] found in %fname%&goto:eof
set /a skipcount0+=1
set "skip0=skip=%skipcount0%"
if %updown% neq 0 (set /a skipcount1=count+updown-1) else goto:findloop
if %skipcount1% lss 0 echo Target line below zero&goto:findloop
if %skipcount1% equ 0 (set "skip1=") else set "skip1=skip=%skipcount1%"
set "variable="
set "found="
for /f "%skip1%tokens=1*delims=:" %%i in ('findstr /n "^" "%fname%"') do if not defined found set "found=%%i"&set "variable=%%j"
if defined found (echo Variable: [%variable%] found at line %found%) else echo Target line after EOF
goto:findloop
endlocal


Post Reply