Search code examples
windowscommand-linetext-files

Append lines of text file with lines from another text file


I only have access to windows and its command line.

I have two text files with many lines, file1 and file2. I want to append the lines in file1 with the corresponding lines from file2.

Each line in file1 looks like this:

apple Orange

Each line in file2 looks like this:

banana

I want the output to look like this:

apple Orangebanana

Any ideas? Preferably via win command prompt?


Solution

  • This uses file1.txt and file2.txt as input files and interleaves them into result.txt

    @echo off
    setlocal DisableDelayedExpansion
    (
    < file2.txt (
       for /F "delims=" %%a in (file1.txt) do (
          set file2Line=
          set /P file2Line=
          set "file1Line=%%a"
          setlocal EnableDelayedExpansion   
          echo(!file1Line!!file2Line!
          endlocal
       )
    )
    )>"result.txt"
    
    pause