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

Posted on 2007-10-11
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
    LVL 10

    Expert Comment

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

    Author Comment

    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

    Ok, what language can this be coded in?
    LVL 92

    Accepted Solution


    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


        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

    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 92

    Assisted Solution

    by:Patrick Matthews
    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

    on linux/unix you simply do

      paste -s a b c
    LVL 10

    Expert Comment

    >>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

    Thank you all

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
    A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now