How to copy text from multiple files up to keyword

March 29, 2012 at 08:50:24
Specs: Windows XP, Intel Core 2 Duo/3.5 GB
I would like to use a DOS batch file to parse multiple lines in multiple text files up to a common keyword and copy their contents to an output file.

So the input files would look like this:

File 1:
Line 1 of text from File 1
Line 2 of text from File 1
Line 3 of text from File 1
Keyword
bla bla bla

File 2:
Line 1 of text from File 2
Keyword
bla bla bla

File 3:
Line 1 of text from File 3
Line 2 of text from File 3
Keyword
bla bla bla

And the desired output would look like:

Line 1 of text from File 1
Line 2 of text from File 1
Line 3 of text from File 1

Line 1 of text from File 2

Line 1 of text from File 3
Line 2 of text from File 3

[Please note that the blank line between the three file outputs is for clarity of example and is not needed for the actual output]

Being a novice at this, here's what I have so far. I stole this from M2GO's many enlightening emails for iterating through each text file:

@echo off > newfile & setLocal enableDELAYedexpansion
for /f "tokens=* delims= " %%a in ('dir/b *.txt') do (
Code needed here to parse through file to "keyword"
)

But I haven't been able to find (or figure out) the code needed within the loop that would parse through each file and output its contents up to when it reaches the "keyword" (and, by the way, the "keyword" is the same string in every file.)

I've been reading lots of email strings in this forum (and others), but I haven't been able to find what I'm looking for. Can any of you learned scholars help? Thanks much!


See More: How to copy text from multiple files up to keyword

Report •

#1
March 29, 2012 at 14:14:46
You would be better off looking for a NT Command Prompt Batch File, MS-DOS died 17 years ago, well before XP...

http://ss64.com/nt/syntax.html

Googling is quicker than waiting for an answer....


Report •
Related Solutions


Ask Question