0

I have a series of text files each named the same in sub-folders of a certain directory

ac.txt files have the following structure :

---

some text

---

[lights]

---

some text

---

[GetEngineData]

---

some text

---

I want to get all those lines in between strings [lights] and [GetEngineData] (including those start [lights] and end [GetEngineData] lines) in one single output file called lights.txt with a blank space in between those coming from each text file.

I coded the following batch yet it is of no avail so far :

@ECHO OFF 
for /r %%a in ('find /n "[lights]"^<(ac.txt) ') do set /a start=%%a
for /r %%a in ('find /n "[GeneralEngineData]"^<(ac.txt) ') do set /a end=%%a
(
    for /r  %%a in ('find /n /v ""^<(ac.txt) ') do (
        IF %%a geq %start% IF %%f leq %end% ECHO(%%b
    )
)>lights.txt
kvari
  • 25
  • 1
  • 7
  • 1
    You can't use `FOR /R` to process the output of a command. Use `FOR /F` to do that. I am not sure what you are trying to do with putting the file mask inside parentheses. – Squashman Nov 10 '16 at 13:39
  • 1
    Possible duplicate of [Batch File - Find two lines then copy everything between those lines](http://stackoverflow.com/questions/33638832/batch-file-find-two-lines-then-copy-everything-between-those-lines) – aschipfl Nov 10 '16 at 14:17
  • You stated that your text files were "all named the same" so why are you looking in every file with a .txt extension and not just in knownfilename.txt? – Compo Nov 10 '16 at 14:31
  • @Compo it doesn't make a difference since there aren't any other text files in the subfolders named differently i.e. *.txt or ac.txt – kvari Nov 10 '16 at 15:10
  • @aschipfl it's for a single file i'm running the batch for all .txt files in subfolders but still may have a peek at it, thanks – kvari Nov 10 '16 at 15:11
  • @aschipfl plus i want to include the start and end strings within my output file. – kvari Nov 10 '16 at 15:17

1 Answers1

1

Here's a way to do it. Might not be the most efficient but it seems to do the job just fine. The code loops through all subfolders and picks up all .TXT files. It then parses each line of each file, marking the beginning/end of each block using the [lights] and [GeneralEngineData] tokens and then outputs everything to res.txt in the same folder where the batch file is stored.

@ECHO OFF 
Setlocal EnableDelayedExpansion
if exist res.txt del res.txt

set inblock=0
for /r . %%a in (*.txt) do (
    set fname=%%a
    for /f "tokens=1* delims=]" %%b in ('type "!fname!" ^| find /n /v ""') do (
        if /i *%%c*==*[lights]* set inblock=1
        if !inblock!==1 (
            if *%%c*==** (echo.) else (echo %%c)
            if /i *%%c*==*[GetEngineData]* set inblock=0
        )
    )
    echo. 
) >> res.txt
set fname=
set inblock=
type res.txt
Filipus
  • 520
  • 4
  • 12
  • i'll give it a go shortly – kvari Nov 11 '16 at 16:17
  • that one works fine, thanks ! just added `/i` in `if` statements to ignore any probable case sensitivity. – kvari Nov 12 '16 at 09:41
  • it only fails to add set of data between `[lights]` and `[GeneralEngineData]` in case there is a blank line right after `[lights]` lines. – kvari Nov 12 '16 at 12:08
  • I just changed the inner loop to account for empty lines (which the FOR command always strips). I had to use a neat little trick explained [in this other SO post](http://stackoverflow.com/questions/8811992/dos-batch-for-loop-with-find-exe-is-stripping-out-blank-lines). Everything should now work as expected. – Filipus Nov 12 '16 at 13:25
  • it handles, this time, all the occurrences of `[lights]` with or without a following blank line but fails to parse the data in between alone, acutally outputting all of the contents. – kvari Nov 12 '16 at 13:48
  • I don't understand what you mean. With the samples files I created, everything in between the [tokens] (including the tokens) gets extracted. Are there files where there might not be an end token? Or files that may have multiple pairs of tokens? It's a bit difficult to determine how to fix it without actually seeing the original data or the results. – Filipus Nov 16 '16 at 04:12
  • I guess the problem arises when there is a blank line underneath the first token [lights]. I'll give it a try again tho, maybe playing around with some stuff a trifle. thanks for your interest and great help – kvari Nov 16 '16 at 16:41
  • i've given it a go again but it's to no avail yet, keeps merging the entire text files into one res.txt, instead of just getting the lines in between and including '[lights]' and '[getenginedata]', unfortunately. – kvari Dec 02 '16 at 18:44
  • You'd have to edit your original post to include actual snippets of files (preferably more than one) as well as the results you get from my batch file in order to figure out what's wrong. Also, make sure when you copy/paste my code to your text editor that the single/double quotes don't get replaced by some other forms of quotes (for instance, « » ). This can happen depending on your locale settings. If in doubt, re-type all quotes and double quotes in the code. – Filipus Dec 05 '16 at 14:21
  • I actually even re-typed all the coding from scratch painstakingly, however it failed again. I'm going to post actual snippet samples in my original post tomorrow. – kvari Dec 05 '16 at 16:00