Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB7
Expertise: Intermediate
Apr 14, 2003

ConcatenateFiles - Concatenating multiple text files

' Concatenate a variable number of text files into a single result file
'
' Params:
'  - resultFile: the complete path of the result file you want to create
'  - header: a string that is written when a file is added to the result file.
'    Note: this string can contain the #FilePath# tag that will be replaced 
' with the path of the file being added
'  - sourceFiles: the sequence of files whose content will be concatenated
'
' Example:
'   ConcatenateFiles ("D:\res.txt", "------ NEW FILE: #FilePath# ------",
'  "D:\1.txt", "D:\2.txt", "D:\3.txt")

Sub ConcatenateFiles(ByVal resultFile As String, ByVal header As String, _
    ByVal ParamArray sourceFiles() As String)
    Dim writer As System.IO.StreamWriter
    Dim reader As System.IO.StreamReader

    Try
        writer = New System.IO.StreamWriter(resultFile, True)
        Dim fpath As String
        For Each fpath In sourceFiles
            ' write the header string, with #FilePath# replaced with
            ' the path of the source file being processed
            writer.WriteLine(header.Replace("#FilePath#", fpath))
            Try
                ' open the source file in read mode
                reader = New System.IO.StreamReader(fpath)
                ' append all the content of the current file in the destination 
                ' file
                writer.Write(reader.ReadToEnd() & Environment.NewLine)
            Finally
                If Not reader Is Nothing Then
                    reader.Close()
                    reader = Nothing
                End If
            End Try
        Next
    Finally
        ' close the writer stream
        If Not writer Is Nothing Then writer.Close()
    End Try
End Sub
Marco Bellinaso
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap