Search code examples
windowsfor-loopconcatenationbatch-processingfindstr

copy each line from 2 files to 1 file Windows batch


I have to files and both have the same amount of lines, and I need to copy each line of both files to a new file, but concatenated. So line 1 of both files need to be copied to line 1 of the new file, and even with additional text in that line. This is part of a bigger Windows batch (cmd.exe).

input file "date.txt":

150102
150103
150104
150105

input file "ID":

ID01
ID02
ID03
ID04

output file "Date_ID.txt" that I need:

150102 is the same as ID01
150103 is the same as ID02
150104 is the same as ID03
150105 is the same as ID04

Could anybody please help me with this? I tried things with /for and findstr but because it is looping I will get every combination of each date with each ID.


Solution

  • @echo off
    setlocal enableDelayedExpansion
    set "file1=path_to\date.txt"
    set "file2=path_to\id.txt"
    set "out=path_to\output.txt"
    
    for /f %%N in ('type "%file1%"^|find /c /v ""') do set "cnt=%%N"
     >"%out%" 9<"%file1%" <"%file2%" (
       for /l %%N in (1 1 %cnt%) do (
        set "ln1="
         set "ln2="
        <&9 set /p "ln1="
        set /p "ln2="
        echo !ln1! is the same as !ln2!
      )
    )
    type "%out%"