Basj
Basj

Reputation: 46393

Concatenate each line of 2 text files

File1.txt is:

Abcd
Efghhh
Ijkl
+1000 other lines

File2.txt is:

1234
1368
QL23372
+1000 other lines

I want to create File3.txt containing:

Abcd$1234
Efghhh$1368
Ijkl$QL23372
+1000 other lines

How to do this in batch or VBscript ?

Upvotes: 0

Views: 324

Answers (4)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38775

A VBScript attempt:

  Dim oFS   : Set oFS   = CreateObject("Scripting.FileSystemObject")
  Dim tsFst : Set tsFst = oFS.OpenTextFile("M:\lib\kurs0705\testdata\filezipper\fst")
  Dim tsSnd : Set tsSnd = oFS.OpenTextFile("M:\lib\kurs0705\testdata\filezipper\snd")
  Do Until tsFst.AtEndOfStream Or tsSnd.AtEndOfStream
     WScript.Echo tsFst.ReadLine() & "$" & tsSnd.ReadLine()
  Loop
  tsSnd.Close
  tsFst.Close

that avoids extra arrays.

Upvotes: 0

Aacini
Aacini

Reputation: 67296

@echo off
setlocal EnableDelayedExpansion

rem Read File1.txt with SET /P command via standard input
< File1.txt (
   rem Read File2.txt with FOR command
   for /F "delims=" %%a in (File2.txt) do (
      set /P line1=
      echo !line1!$%%a
   )
rem Send output to File3.txt
) > File3.txt

This Batch program will fail if the input files contain empty lines. It also will fail with special Batch characters, like ! < | >. These limitations may be fixed, if needed.

Upvotes: 1

Alexander
Alexander

Reputation: 4173

Solution using *.bat file:

setlocal EnableDelayedExpansion

set i=0
for /f "delims=" %%A in (file1.txt) do (
    set /A i+=1
    set arr1[!i!]=%%A
)

set i=0
for /f "delims=" %%A in (file2.txt) do (
    set /A i+=1
    set arr2[!i!]=%%A
)

for /L %%i in (1,1,%i%) do @echo !arr1[%%i]!$!arr2[%%i]!>> result.txt

Upvotes: 0

Jerry
Jerry

Reputation: 4408

Assuming equal number of lines for both files:

string[] lines1 = File.ReadAllLines("File1.txt");
string[] lines2 = File.ReadAllLines("File2.txt");

string[] lines3 = new string[lines1.Length];
for(int i=0;i<lines1.Length;i++)
    lines3[i]=lines1[i]+"$"+lines2[i];
File.WriteAllLines("File3.txt",lines3);

Upvotes: 1

Related Questions