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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user2483876
  • 129
  • 3
  • 13

1 Answers1

7
@echo off
for /f "delims=" %%a in (a.txt) do (
    for /f "delims=" %%b in (b.txt) do (
        >>c.txt echo %%a %%b
    )
)
Simon MᶜKenzie
  • 8,344
  • 13
  • 50
  • 77
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • +1 It is funny that what the OP calls "the tricky part" is precisely the requirement that makes this solution so simple! **;-)** – Aacini Jun 14 '13 at 01:58
  • Thanks - works perfectly! This was so much simpler than I thought it would be! I'm obviously out of touch :/ – user2483876 Jun 14 '13 at 05:49
  • 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 – Rakha Oct 13 '17 at 14:19