Learn how to a build a cloud-first strategyRegister Now


Multiple text files into one text file with the lines in order

Posted on 2007-10-11
Medium Priority
Last Modified: 2010-04-16
I have multiple text files and I want to take their lines in order and place them into one new text file.

file a line 1
file b line 1
file c line 1
file a line 2
file b line 2
file c line 2

I have very minor scripting knowledge, an annotated code would be much appreciated.
Question by:mgjust
  • 4
  • 3
  • 2
  • +1
LVL 10

Expert Comment

ID: 20059629
How many files are there, and how many lines in each one - max upper limit will do.

Author Comment

ID: 20059679
There are 3 files right now each with 71 lines. It'd be nice, but not necessary to be able to adjust these numbers. Thank you.
LVL 10

Expert Comment

ID: 20059784
Ok, what language can this be coded in?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 93

Accepted Solution

Patrick Matthews earned 1640 total points
ID: 20059838

Dim fso, tsA, tsB, tsC, tsResult

Set fso = CreateObject("Scripting.FileSystemObject")
Set tsA = fso.OpenTextFile("c:\a.txt")
Set tsB = fso.OpenTextFile("c:\b.txt")
Set tsC = fso.OpenTextFile("c:\c.txt")
Set tsResult = fso.CreateTextFile("c:\result.txt", True)

Do Until tsA.AtEndOfStream And tsB.AtEndOfStream And tsC.AtEndOfStream
    If Not tsA.AtEndOfStream Then tsResult.WriteLine tsA.ReadLine
    If Not tsB.AtEndOfStream Then tsResult.WriteLine tsB.ReadLine
    If Not tsC.AtEndOfStream Then tsResult.WriteLine tsC.ReadLine


Set tsA = Nothing
Set tsB = Nothing
Set tsC = Nothing
Set tsResult = Nothing
Set fso = Nothing

MsgBox "Done"
LVL 10

Assisted Solution

peetm earned 80 total points
ID: 20059877

    Dim fso As New FileSystemObject
    Dim sa1() As String
    Dim sa2() As String
    Dim sa3() As String
    sa1 = Split(fso.GetFile("c:\file1.txt").OpenAsTextStream(ForReading).ReadAll, vbCrLf)
    sa2 = Split(fso.GetFile("c:\file2.txt").OpenAsTextStream(ForReading).ReadAll, vbCrLf)
    sa3 = Split(fso.GetFile("c:\file3.txt").OpenAsTextStream(ForReading).ReadAll, vbCrLf)

    Dim n As Integer
    Dim s As String
    ' Assuming all the file have the same number of lines in them
    For n = 0 To UBound(sa1)

        s = s & sa1(n) & vbCrLf
        s = s & sa2(n) & vbCrLf
        s = s & sa3(n) & vbCrLf
    Next n

    Call fso.CreateTextFile("c:\interleaved.txt", True).Write(s)

Author Comment

ID: 20060076
I was looking for python or vb:
matthewspatrick: thanks it worked.
peetm: I get an error "expected end of statement" for line: 1 char: 11.

I would appreciate a short annotation of what the code commands are, since I dont know what they mean.  I don't know why either of the above is better, it doesn't really matter, I'd just like to have a basic idea of what's going on.
LVL 93

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 1640 total points
ID: 20060201
mgjust said:
>>matthewspatrick: thanks it worked.

Cool.  BTW, that script should work even if the files have different numbers of lines.  Up until
the Do, it's pretty self-explanatory.

The Do condition is "end of file A" AND "end of file B" AND "end of file C"; until all of those
are true it keeps looping.  Inside the loop, it tests to see if we are at the end of File A; if not
then we write the current line from File A to the result file.  And so on for Files B & C.
LVL 51

Assisted Solution

ahoffmann earned 80 total points
ID: 20060774
on linux/unix you simply do

  paste -s a b c
LVL 10

Expert Comment

ID: 20060792
>>peetm: I get an error "expected end of statement" for line: 1 char: 11.

Yup - the code is VB, not vbs.  If the other posted code - which was vbs - works for you, just go with that.

Author Comment

ID: 20061173
Thank you all

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to dynamically set the form action using jQuery.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month20 days, 22 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question