| {\rtf1\ansi\ansicpg1252\deff0\deflang1044{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}} \viewkind4\uc1\pard\f0\fs17 Often when distribution files you may need to split the files into manageable chunks eg. to fit on a floppy disk. The following routines demonstrate how to split and rejoin files of any type: \par \par \par Option Explicit \par \par 'Purpose : Joins the contents of two files together. \par 'Inputs : sFirstFileName The path and file name of the first file. \par ' sSecondFileName The path and file name of the first file. \par ' [sNewFileName] The path and file name of the resulting file. \par ' If this is not specified then the contents of \par ' sSecondFileName are appended to sFirstFileName. \par 'Outputs : Returns True on success. \par 'Notes : Works with any file format. \par \par Function FilesJoin(ByVal sFirstFileName As String, ByVal sSecondFileName As String, Optional ByVal sNewFileName As String) As Boolean \par Dim abBuffer() As Byte, ihFirstFile As Integer, ihSecondFile As Integer, ihNewFile As Integer \par Dim bCreateNew As Boolean \par \par On Error GoTo ErrFailed \par 'Open the second file \par bCreateNew = CBool(Len(sNewFileName)) \par ihSecondFile = FreeFile \par Open sSecondFileName For Binary Access Read As #ihSecondFile \par \par If bCreateNew Then \par 'Open first file to read \par ihFirstFile = FreeFile \par Open sFirstFileName For Binary Access Read As #ihFirstFile \par \par 'Create a New file to hold the contents of the first and second file. \par ihNewFile = FreeFile \par Open sNewFileName For Binary Access Write As #ihNewFile \par \par 'Read and write the first file to the output file \par ReDim abBuffer(1 To LOF(ihFirstFile)) \par Get #ihFirstFile, , abBuffer \par Put #ihNewFile, , abBuffer \par \par 'Read and write the second file to the output file \par ReDim abBuffer(1 To LOF(ihSecondFile)) \par Get #ihSecondFile, , abBuffer \par Put #ihNewFile, , abBuffer \par Else \par 'Append the contents of the second file to first file \par ihFirstFile = FreeFile \par Open sFirstFileName For Binary Access Write As #ihFirstFile \par ReDim abBuffer(1 To LOF(ihSecondFile)) \par Get #ihSecondFile, , abBuffer \par 'Append to end of file \par Put #ihFirstFile, LOF(ihFirstFile) + 1, abBuffer \par End If \par \par 'Close files \par Close #ihFirstFile, #ihSecondFile, #ihNewFile \par FilesJoin = True \par Exit Function \par \par ErrFailed: \par Debug.Print "File Split Error: " & Err.Description \par FilesJoin = False \par Close #ihFirstFile, #ihSecondFile, #ihNewFile \par End Function \par \par \par \par 'Purpose : Splits a file into sections of a specific size \par 'Inputs : sFileName The path and file name of the file to split. \par ' lFileSplitSize The size of the individual files to be created by this function (in bytes). \par ' [bOverwrite] If True overwrites existing files when creating the output files. \par 'Outputs : Returns the number of files it created or -1 on error. \par 'Notes : Works with any file format. \par ' An example of the output files created is: \par ' Original file name "C:\\Test.txt" \par ' Output files "C:\\Test.txt.1", "C:\\Test.txt.2" etc \par \par Function FileSplit(ByVal sFileName As String, ByVal lFileSplitSize As Long, Optional bOverwrite As Boolean = False) As Long \par Dim abBuffer() As Byte, ihFile As Integer, ihNewFile As Integer, lThisByte As Long, lFileBytes As Long \par \par On Error GoTo ErrFailed \par 'Open the file \par ihFile = FreeFile \par Open sFileName For Binary Access Read As #ihFile \par lFileBytes = LOF(ihFile) \par ReDim abBuffer(1 To lFileSplitSize) \par \par For lThisByte = 1 To lFileBytes Step lFileSplitSize \par 'Read data in from existing file \par Get #ihFile, lThisByte, abBuffer \par 'Open output file \par ihNewFile = FreeFile \par FileSplit = FileSplit + 1 \par If bOverwrite Then \par If Len(Dir$(sFileName & "." & FileSplit)) > 0 Then \par 'File exists, delete it \par Kill sFileName & "." & FileSplit \par End If \par End If \par Open CStr(sFileName & "." & FileSplit) For Binary Access Write As #ihNewFile \par 'Write data to output file \par Put #ihNewFile, , abBuffer \par 'Close output file \par Close #ihNewFile \par Next \par \par Close #ihFile \par Exit Function \par \par ErrFailed: \par Debug.Print "File Split Error: " & Err.Description \par FileSplit = -1 \par Close #ihFile, #ihNewFile \par End Function \par \par \par 'Demonstration routine \par 'Splits a file and joins it back together \par Sub Test() \par Dim lNumFiles As Long, lThisFile As Long \par 'Split a file into sections of 10 kb \par lNumFiles = FileSplit("C:\\Test1.txt", 10 * 1024, True) '10 kb per file \par 'Rejoin the files \par For lThisFile = 1 To lNumFiles \par FilesJoin "C:\\Test New.txt", "C:\\Test1.txt." & lThisFile \par Next \par End Sub \par } |
Splitting and joining files |
India web developer web development India | India web development company India ecommerce web developer