Interleave files with CMD using echo

2020-05-08 21:37发布

问题:

How could I interleave files line by line with

cmd.exe

with help of read, grep, echo, etc?

File1.txt with the context:

Aaaaaaa1 
Aaaaaaa2 
Aaaaaaa3

File2.txt with the context:

Bbbbbbb1
Bbbbbbb2
Bbbbbbb3

I would like to combine File1.txt and File2.txt in other file (OUTCOME.txt) in this way:

Aaaaaaa1
Bbbbbbb1
Aaaaaaa2
Bbbbbbb2
Aaaaaaa3
Bbbbbbb3

回答1:

You need a method to read from two files in parallel. This is possible by using two methods at the same time (<file1 set /p and for /f ... in (file2)):

@echo off
setlocal enabledelayedexpansion

<file2.txt (
  for /f "delims=" %%a in (file1.txt) do (
    set /p b=
    echo %%a
    echo !b!
  )
) >outcome.txt