James Harvey
James Harvey

Reputation: 23

VBSCRIPT to read multi files into an array then write them to a single file

As the title suggests, I have three separate text files that I want to join together in a certain order (i.e., append file1, file2, file3 (in order) to make file4).

From what I've read, to do this with VBScript would require the FileSystemObject to read the files into an array then write the contents to the new file (I am open to whatever works with VBScript if suggested)

I'm having the following issues with my code: 1) The script runs, but produces no data 2) After I get it to run, it is imperative that the files append to the output file in the order of the array in the order (per line) I suggest above.

Here is the Array example I'm working with :

CODE

Const ForReading = 1


Dim arrServiceList(2)
arrServiceList(0) = strText1
arrServiceList(1) = strText2
arrServiceList(2) = strText3


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFSO.CreateTextFile("output.txt")


Set objTextFile1 = objFSO.OpenTextFile("C:\Users\95540\Desktop\Sample1.txt", ForReading)
Set objTextFile2 = objFSO.OpenTextFile("C:\Users\95540\Desktop\Sample2.txt", ForReading)
Set objTextFile3 = objFSO.OpenTextFile("C:\Users\95540\Desktop\Sample3.txt", ForReading)

    strText1 = objTextFile1.ReadAll
    objTextFile1.Close

    strText2 = objTextFile2.ReadAll
    objTextFile2.Close

    strText3 = objTextFile3.ReadAll
    objTextFile3.Close


    objOutputFile.WriteLine arrServiceList(0)
    objOutputFile.Close

====================

UPDATE TO MY CODE 5-15-15 (Description of corrections in below post)

CODE

   Const ForReading = 1


  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objOutputFile = objFSO.CreateTextFile("output.txt")


  Set objTextFile1 = objFSO.OpenTextFile("C:\Users\Brill\Desktop\Grab1.txt", ForReading)
  Set objTextFile2 = objFSO.OpenTextFile("C:\Users\Brill\Desktop\Grab2.txt", ForReading)
  Set objTextFile3 = objFSO.OpenTextFile("C:\Users\Brill\Desktop\Grab3.txt", ForReading)

  Do While objTextFile1.AtEndOfStream <> True
  Do While objTextFile2.AtEndOfStream <> True
  Do While objTextFile3.AtEndOfStream <> True

    strText1 = objTextFile1.ReadLine
    objOutputFile.Write strText1 & vbTab 

    strText2 = objTextFile2.ReadLine
    objOutputFile.Write strText2 & vbTab 

    strText3 = objTextFile3.ReadLine
    objOutputFile.Write strText3 & vbTab &  vbCrLf

  Loop
  Loop
  Loop

objOutputFile.Close

objTextFile1.Close
objTextFile2.Close
objTextFile3.Close   

Upvotes: 1

Views: 3274

Answers (2)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38755

Merging/Zipping more then one collection (e.g. some 'column files') into one collection (e.g. a 'table file') is a standard problem with a standard solution strategy (which doesn't involve reading "the files into an array" at all).

This demo code:

Option Explicit
Dim goFS  : Set goFS  = CreateObject("FileSystemObject")
Dim oFZip : Set oFZip = New cFZip
oFZip.m_aIFSpecs = Split("..\data\a.txt ..\data\b.txt ..\data\c.txt")
oFZip.zip "..\data\abc.txt"
WScript.Echo goFS.OpenTextFile("..\data\abc.txt").ReadAll()

Class cFZip
  Public m_aIFSpecs ' array of input files
  Function zip(sOFSpec)
    Dim tsOut : Set tsOut = goFS.CreateTextFile(sOFSpec)
    Dim nUBFiles : nUBFiles = UBound(m_aIFSpecs)
    ReDim aFiles(nUBFiles)
    Dim f
    For f = 0 To nUBFiles
        Set aFiles(f) = goFS.OpenTextFile(m_aIFSpecs(f))
    Next
    Dim bDone
    Do
       Redim aData(UBound(m_aIFSpecs))
       bDone = True
       For f = 0 To nUBFiles
           If Not aFiles(f).AtEndOfStream Then
              bDone = False
              aData(f) = aFiles(f).ReadLine()
           End If
       Next
       If Not bDone Then tsOut.WriteLine Join(aData, ",")
    Loop Until bDone
    For f = 0 To nUBFiles
        aFiles(f).Close
    Next
    tsOut.Close
  End Function
End Class

output:

1,10,100
2,20,200
3,30,300
4,,400
,,500

shows the basic approach. I use a Class to make experiments/specific adaptions (e.g. delimiter, quoting, ...) easier.

Upvotes: 2

Dijkgraaf
Dijkgraaf

Reputation: 11537

The below works. Problems with your script. 1. You were assigning the variables to the array before you had populated them. 2. You were not writing all the elements of the array.

Const ForReading = 1


Dim arrServiceList(2)


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFSO.CreateTextFile("output.txt")


Set objTextFile1 = objFSO.OpenTextFile("C:\Users\95540\Desktop\Sample1.txt", ForReading)
Set objTextFile2 = objFSO.OpenTextFile("C:\Users\95540\Desktop\Sample2.txt", ForReading)
Set objTextFile3 = objFSO.OpenTextFile("C:\Users\95540\Desktop\Sample3.txt", ForReading)

    strText1 = objTextFile1.ReadAll
    objTextFile1.Close

    strText2 = objTextFile2.ReadAll
    objTextFile2.Close

    strText3 = objTextFile3.ReadAll
    objTextFile3.Close

    arrServiceList(0) = strText1
    arrServiceList(1) = strText2
    arrServiceList(2) = strText3


    objOutputFile.WriteLine arrServiceList(0)
    objOutputFile.WriteLine arrServiceList(1)
    objOutputFile.WriteLine arrServiceList(2)
    objOutputFile.Close

Upvotes: 1

Related Questions