Batch file conbine text files

November 8, 2010 at 15:16:36
Specs: Windows XP
First off I should check the title of my post for spelling errors before I hit post next time. Apparently you can't change the title after you post it, you just get the embarrassment of knowing you can't spell :) So let me try this again:


BATCH FILE COMBINE TEXT FILES

I need to combine 4 files together to get some htm code. The files are straight forward:

1. text1.txt contains multiple lines of code
2. text2.txt contains a single line of code
3. filename1.txt contains a list of file names in rows
4. filename2.txt contains another list of filenames in rows corresponding to filename1.txt

What I need is to combine the files in the following way:
1. entire file text1.txt
2. row1 of filename1.txt
3. row1 of filename2.txt
4. entire file text2.txt
5. repeat for all rows of filename1 and 2. (several hundreds of times)

The object of what I am trying to do is shown in the desired output below:

Let's assume text1.txt has the following 3 lines of code:
line1 from text1.txt
line2 from text1.txt
line3 from text1.txt

Final output:

line1 from text1.txt
line2 from text1.txt
line3 from text1.txt
row1 filename1.txt
row1 filename2.txt
line 1 from text2.txt (only 1 line of code)
line1 from text1.txt
line2 from text1.txt
line3 from text1.txt
row2 filename1.txt
row2 filename2.txt
line 1 from text2.txt (only 1 line of code)
Etc...

I feel like this should be easy but have been stumped thus far. Thanks in advance.


See More: Batch file conbine text files

Report •


#1
November 8, 2010 at 16:58:55
Do these have the same number of rows?

filename1.txt
filename2.txt


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
November 9, 2010 at 05:17:24
Yes. filename1.txt and filename2.txt have exactly the same number of rows and correspond row for row (row1 of filename1 goes with row 1 of filename2, etc). Essentially filename2.txt is a variation of filename1.txt.

Report •

#3
November 10, 2010 at 16:07:31
@echo off > newfile & setLocal enableDELAYedeXpansion

set N=
for /f "tokens=* delims= " %%a in (filename1.txt) do (
set /a N+=1
set O!N!=%%a
)

set N=
for /f "tokens=* delims= " %%a in (filename2.txt) do (
set /a N+=1
set T!N!=%%a
)

for /L %%i in (1 1 !N!) do (
type text1.txt
echo.!O%%i!
echo.!T%%i!
type text2.txt
) >> newfile


=====================================
Life is too important to be taken seriously.

M2


Report •

Related Solutions

#4
November 11, 2010 at 11:37:31
That worked like a charm and saved me many hours of unnecessary tediousness. Thanks again.

ryan


Report •


Ask Question