3

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.

1 Answer 1

7
@echo off
for /f "delims=" %%a in (a.txt) do (
    for /f "delims=" %%b in (b.txt) do (
        >>c.txt echo %%a %%b
    )
)
Sign up to request clarification or add additional context in comments.

3 Comments

+1 It is funny that what the OP calls "the tricky part" is precisely the requirement that makes this solution so simple! ;-)
Thanks - works perfectly! This was so much simpler than I thought it would be! I'm obviously out of touch :/
foxidrive, this is a nice loop, what if one would like, with the same files given as example: 1 a 2 b 3 c ... instead of what it is now: 1 a 1 b 1 c 1 d 1 e 2 a 2 b 2 c

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.