Visual Basic Programming Code Examples Visual Basic > Files Directories Drives Code Examples Join the contents of two files together Join the contents of two files together The routine below joins the contents of two files together: Option Explicit 'Purpose : Joins the contents of two files together 'Inputs : sFirstFileName The file name and path to the first file ' sSecondFileName The file name and path to the second file ' [sNewFileName] The file name and path to the new file. If ' no value is specified then the second file is ' added to the first file. 'Outputs : Returns True if an error occurred 'Notes : The files can be of any type. ' In the MSDOS command prompt you can type the following to join files together: ' type *.txt >> ALL.txt 'Revisions : Function FilesJoin(ByVal sFirstFileName As String, ByVal sSecondFileName As String, Optional ByVal sNewFileName As String) As Boolean Dim abBuffer() As Byte, iFirstFile As Integer, iSecondFile As Integer, iNewFile As Integer Dim bCreateNew As Boolean On Error GoTo ErrFailed 'Open the file handles bCreateNew = CBool(Len(sNewFileName)) iFirstFile = FreeFile Open sFirstFileName For Binary Access Read As #iFirstFile iSecondFile = FreeFile Open sSecondFileName For Binary Access Read As #iSecondFile iNewFile = FreeFile If bCreateNew Then 'Create a New file Open sNewFileName For Binary Access Write As #iNewFile 'Size the array to hold the file contents ReDim abBuffer(1 To LOF(iFirstFile)) 'Read the file contents Get #iFirstFile, , abBuffer Put #iNewFile, , abBuffer 'Size the array to hold the file contents ReDim abBuffer(1 To LOF(iSecondFile)) 'Read the file contents Get #iSecondFile, , abBuffer Put #iNewFile, , abBuffer Else 'Join SecondFile to FirstFile Open sFirstFileName For Binary Access Write As #iFirstFile 'Size the array to hold the file contents ReDim abBuffer(1 To LOF(iSecondFile)) 'Read the file contents Get #iSecondFile, , abBuffer Put #iFirstFile, , abBuffer End If 'Close the file handles Close #iFirstFile, #iSecondFile, #iNewFile 'Close the files Exit Function ErrFailed: 'Failed to join files Close #iFirstFile, #iSecondFile, #iNewFile FilesJoin = True End Function