user2483876
user2483876

Reputation: 139

Merge Two text files line by line using batch script

I have 2 text files; A.txt and B.txt and I want to merge them to make C.txt using a batch script.

However (here's the tricky part) I wish to do it so each line from A.txt is appended with a space then the first line from B.txt then a new line with the first line from A and the second from B and so on until the end of B is reached and then we start with the second line of A.

I know I haven't worded this well so here's an example:

A.txt;

1
2
3
4
5

B.txt;

Z
Y
X
W
V
T
R

So C.txt would have;

1 Z
1 Y
1 X
1 W
1 V
1 T
1 R
2 Z
2 Y

etc.

Upvotes: 3

Views: 6565

Answers (1)

foxidrive
foxidrive

Reputation: 41224

@echo off
for /f "delims=" %%a in (a.txt) do (
    for /f "delims=" %%b in (b.txt) do (
        >>c.txt echo %%a %%b
    )
)

Upvotes: 7

Related Questions