Daniel
Daniel

Reputation: 3

Batch string search iterating over all files in a folder

I have the code below. It may be messy but it works on one file at a time, in this case test1.OUT. What I am trying to do is to try and use some sort of wildcard name instead of test1.OUT and iterate the batch file over all .OUT files in a folder.

The other issue that I would run in to is that the output3.txt file would be overwritten each time. Is it possible to have each run of the batch file export the information and add it to output3.txt rather than overwritting previous information?

@echo off
setlocal EnableDelayedExpansion

rem Assemble the list of line numbers
set numbers=
for /F "delims=:" %%a in ('findstr /I /N /C:"Number of Error Taps:  0" test1.OUT') do (
    set /A before=%%a-6, after=%%a+1
    set "numbers=!numbers!!before!: !after!: "
)
rem Search for the lines
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" test1.OUT ^| findstr /B      "%numbers%"') do echo %%b) > output.txt

set wildcard=%%G
set numbers=
for /F "delims=:" %%a in ('findstr /I /N /C:"Site Number:" test1.OUT') do (
    set /A before=%%a-1, after=%%a+1
    set "numbers=!numbers!!before!: !after!: "
)
rem Search for the lines
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" test1.OUT ^| findstr /B         "%numbers%"') do echo %%b) > output1.txt


set numbers=
for /F "delims=:" %%a in ('findstr /I /N /C:"Number of Error Taps:  0" test1.OUT') do (
    set /A before=%%a-50, after=%%a+1
    set "numbers=!numbers!!before!: !after!: "
)
rem Search for the lines
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" test1.OUT ^| findstr /B   "%numbers%"') do echo %%b) > output2.txt

copy output1.txt+output.txt+output2.txt output3.txt
@pause

Upvotes: 0

Views: 152

Answers (1)

Endoro
Endoro

Reputation: 37569

example code:

@echo off &setlocal EnableDelayedExpansion

for %%a in (*.out) do call:process "%%~a"
goto:eof

:process
set numbers=
for /F "delims=:" %%a in ('findstr /I /N /C:"Number of Error Taps:  0" "%~1"') do (
    set /A before=%%a-6, after=%%a+1
    set "numbers=!numbers!!before!: !after!: "
)

(for /F "tokens=1* delims=:" %%a in ('findstr /N "^"  "%~1" ^| findstr /B "%numbers%"') do echo %%b)> "%~n1.txt"

set numbers=
for /F "delims=:" %%a in ('findstr /I /N /C:"Site Number:"  "%~1"') do (
    set /A before=%%a-1, after=%%a+1
    set "numbers=!numbers!!before!: !after!: "
)

(for /F "tokens=1* delims=:" %%a in ('findstr /N "^"  "%~1" ^| findstr /B "%numbers%"') do echo %%b)> "%~n11.txt"

set numbers=
for /F "delims=:" %%a in ('findstr /I /N /C:"Number of Error Taps:  0" "%~1"') do (
    set /A before=%%a-50, after=%%a+1
    set "numbers=!numbers!!before!: !after!: "
)

(for /F "tokens=1* delims=:" %%a in ('findstr /N "^"  "%~1" ^| findstr /B "%numbers%"') do echo %%b)> "%~n12.txt"

copy  "%~n11.txt" + "%~n1.txt" +  "%~n12.txt" = "%~n13.txt"

exit /b

Upvotes: 1

Related Questions