Tuesday, December 04, 2007

Combining Text Files using VBA

Visual Basic Application to Merge Text Files

Multiple utilities are available to split & merge text files. However, here is a simple one my friend uses to merge around 30 ascii files into one

It uses File System Object and you need to add a reference of Microsoft Scripting Runtime

Sub Append_Text_Files()

Dim oFS As FileSystemObject
Dim oFS1 As FileSystemObject

Dim oTS As TextStream
Dim oTS1 As TextStream

Dim vTemp

Set oFS = New FileSystemObject
Set oFS1 = New FileSystemObject


For i1 = 1 To 30

Set oTS = oFS.OpenTextFile("c:\Sheet" & i1 & ".txt", ForReading)
vTemp = oTS.ReadAll

Set oTS1 = oFS.OpenTextFile("c:\CombinedTemp.txt", ForAppending, True)
oTS1.Write (vTemp)

Next i1

End Sub

The code is simple.. it searches for files from Sheet1.txt ...Sheet30.txt and copies the content into one variable. Then it appends the content to CombinedTemp.txt

5 comments:

  1. Anonymous11:59 AM

    May be more efficient if you move this line to the outside of the loop:

    Set oTS1 = oFS.OpenTextFile("c:\CombinedTemp.txt", ForAppending, True)

    ReplyDelete
  2. Thank you very much. I am able to start with the above post and solve a problem with 105 text files in about 30 minutes.

    I have to add an If statement to skip some non-existing file names. Also have to add .close command each time after the cumulative file is opened and appended.

    Thanks again.

    Sub Append_Text_Files()

    Dim oFS As FileSystemObject
    Dim oFS1 As FileSystemObject

    Dim oTS As TextStream
    Dim oTS1 As TextStream

    Dim vTemp

    Set oFS = New FileSystemObject
    Set oFS1 = New FileSystemObject


    For intCounter = 26 To 1231

    If Dir("C:\FileAppend\SVExport_" & intCounter & "2009.txt") <> "" Then
    Set oTS = oFS.OpenTextFile("C:\FileAppend\SVExport_" & intCounter & "2009.txt", ForReading)
    vTemp = oTS.ReadAll

    Set oTS1 = oFS.OpenTextFile("C:\FileAppend\CombinedTemp.txt", ForAppending, True)
    oTS1.Write (vTemp)
    oTS1.Close
    End If

    Next intCounter

    End Sub

    ReplyDelete
  3. Anonymous1:20 PM

    Can you do this in Excel VBA?

    ReplyDelete
  4. A Plain Text Editor
    Plain Text files
    That's right, if you're writer on a budget, you don't need to spend any money buying expensive writing software or apps. Instead, you can use the text editor that comes free with your operating system.
    Just open up Notepad on Windows or TextEdit on a Mac. I like plain text editors for writing something short quickly and easily, without thinking much about it. I wrote a blog post about the benefits of using plain text editors as writing software.
    Use for: writing whatever, wherever

    ReplyDelete
  5. Nice Blog. Thanks for sharing this Useful Blog on how to Combining Test Files. We can Also use Synkronizer Excel Tool to Compare and Merge excel files into one file.

    ReplyDelete

StumbleUpon
Share on Facebook
Related Posts Plugin for WordPress, Blogger...
Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.