Search code examples
batch-fileconcatenationstring-concatenation

Batch file - concatenate two .txt files to one


I have some difficulties concatenating two text files. First text file

Document1
Document2
Document3

Second text file

456
233
100

What I want to know is how to concatenate them in another text file so that so they can be seen this way

Document1-456
Document2-233
Document3-100

I have this code but it does not work as I want

@echo off
Set f1=file1.txt
set f2=file2.txt

FOR /F "Delims=" %%A in ('TYPE %f1%') DO (
    FOR /F "Delims=" %%B in ('TYPE %f2%') DO (
        echo.%%A-%%B >>newfile.txt
    )
)

I hope you can help me


Solution

  • Reference

    @Echo Off & SetLocal EnableDelayedExpansion
    Set "FileA=File1.txt"
    Set "FileB=File2.txt"
    <%FileB% (For /f "delims=" %%A in (%FileA%) Do (
        Set "B="&Set /P "B="
        Echo:%%A-!B!
    )) >newfile.txt