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

I have multiple text files and I want to take their lines in order and place them into one new text file.

e.g.
file a line 1
file b line 1
file c line 1
file a line 2
file b line 2
file c line 2
etc

I have very minor scripting knowledge, an annotated code would be much appreciated.
Thanks,
mgjustAsked:
Who is Participating?
 
Patrick MatthewsConnect With a Mentor Commented:
VBScript:



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
Loop

tsA.Close
tsB.Close
tsC.Close
tsResult.Close

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

MsgBox "Done"
0
 
peetmCommented:
How many files are there, and how many lines in each one - max upper limit will do.
0
 
mgjustAuthor Commented:
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.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
peetmCommented:
Ok, what language can this be coded in?
0
 
peetmConnect With a Mentor Commented:
VB:

    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)
0
 
mgjustAuthor Commented:
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.
0
 
Patrick MatthewsConnect With a Mentor Commented:
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.
0
 
ahoffmannConnect With a Mentor Commented:
on linux/unix you simply do

  paste -s a b c
0
 
peetmCommented:
>>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.
0
 
mgjustAuthor Commented:
Thank you all
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.