VB and VBA Users Source Code: Join the contents of two files together
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Join the contents of two files together
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Wednesday, December 20, 2000
Hits:
567
Category:
Files/Directories/IO
Article:
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 'Author : Andrew Baker 'Date : 25/11/2000 03:17 '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
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder