variables - Extract line below Findstr criteria in Batch file for cycle -
i'm trying extract set of txt files line placed 2 rows below 1 matching search criteria , redirect output csv file.
i managed specific txt file in set, i'm getting troubles in writing cycle batch-scan each txt in given folder.
through this, wrote following code scan a specific file. works fine:
setlocal enabledelayedexpansion cd "myfolder" if exist myoutput.csv del myoutput.csv /f "delims=:" %%a in ('findstr /b /n /c:"mycriteria" "myfile.txt"') ( set /a linebelow=%%a+2 set "linebelow=!linebelow!: " ) (for /f "tokens=1* delims=:" %%a in ('findstr /n "^" "myfile.txt" ^| findstr /b "%linebelow%"') ^ echo %%b>>myoutput.csv) start myoutput.csv endlocal
when tried generalize code in cycle scan each txt in myfolder got error in findstr: !linebelow! happens empty variable...
here's flawed cycle:
setlocal enabledelayedexpansion cd "myfolder" if exist myoutput.csv del myoutput.csv %%f in ("*.txt") ( ( /f "delims=:" %%a in ('findstr /b /n /c:"mycriteria" "%%f"') ( set /a linebelow=%%a+2 set "linebelow=!linebelow!: " )) (for /f "tokens=1* delims=:" %%a in ('findstr /n "^" "%%f" ^| findstr /b "!linebelow!"') ^ echo %%b>>myoutput.csv)) start myoutput.csv endlocal
could me in correcting code? thanks
@echo off setlocal enableextensions enabledelayedexpansion cd "myfolder" > myoutput.csv ( /f "tokens=1,2 delims=:" %%a in (' findstr /b /n /c:"mycriteria" *.txt ') ( set /a "line=%%b+2" /f "tokens=1,* delims=:" %%c in (' findstr /n "^" "%%a" ^| findstr /b /c:"!line!:" 2^>nul ') echo(%%d ) )
this uses findstr
directly enumerate lines matching mycriteria , include in output name of file (%%a
) , number of line (%%b
). information used retrieve final line (%%d
) matching files.
Comments
Post a Comment