| You can use the CFileCopy class to monitor file copy progress, and even concatenate files into a single destination. There's little difference between copying a single file and concatenating two or more to a single destination. You simply keep the destination file open, while repeatedly opening, reading, and writing source files. Public Function ConcatFiles(ByVal Destination _ As String, ParamArray Source()) As Boolean Dim hSource As Long Dim hDest As Long Dim Src As Variant ' Handle special cases of 0 or 1 file(s) Select Case UBound(Source) Case -1 ' No files passed, this would be a ' problem Exit Function Case 0 ' Single file ConcatFiles = Me.CopyFile(Source(0), _ Destination) Exit Function End Select ' Check destination - use first source ' filename if none given If IsDirectory(Destination) Then Destination = Destination & _ IIf(Right(Destination, 1) = "\", "", _ "\") & ExtractName(Source(0)) End If ' Get rid of existing file If IsFile(Destination) Then If KillFile(Destination) = False Then ' Error: Can't kill destination Exit Function End If End If ' Initialize tracking variables Src = Source m_BytesToCopy = SumBytes(Src) m_BytesCopied = 0 m_FilesToCopy = UBound(Source) + 1 m_FilesCopied = 0 m_Cancel = False ' Enable error trap On Error GoTo CopyFailed ' Open Destination hDest = FreeFile Open Destination For Binary Access Write As _ #hDest For Each Src In Source ' Check source If IsFile(Src) Then ' Open this source hSource = FreeFile Open Src For Binary Access Read As _ #hSource ' Sling bits and close source Call CopyBits(hSource, hDest) Close #hSource End If Next Src ' Clean up Close #hDest ' Delete partial file if user cancelled If m_Cancel Then KillFile Destination ' If we made it here, we probably succeeded! ConcatFiles = (m_Cancel = False) Exit Function CopyFailed: ' Error: Should be recorded and reported End Function Private Function CopyBits(ByVal hSource As _ Long, ByVal hDest As Long) As Boolean Dim nSize As Long, Buffer() As Byte Dim nCopied As Long, i As Long Const BuffSize As Long = 32 * 1024& ' Need filesize for some calcs nSize = LOF(hSource) ' Loop through one buffer at a time If nSize >= BuffSize Then ReDim Buffer(1 To BuffSize) As Byte For i = 1 To (nSize \ BuffSize) Get #hSource, , Buffer Put #hDest, , Buffer Call UpdateProgress(BuffSize) If m_Cancel Then Exit For Next i End If ' Get last chunk of file If m_Cancel = False Then If nSize Mod BuffSize Then ReDim Buffer(1 To (nSize Mod _ BuffSize)) As Byte Get #hSource, , Buffer Put #hDest, , Buffer Call UpdateProgress(UBound(Buffer)) End If End If ' Increment counter If m_Cancel = False Then m_FilesCopied = m_FilesCopied + 1 End If End Function Private Sub UpdateProgress(ByVal nCopied As Long) ' Let client know about progress m_BytesCopied = m_BytesCopied + nCopied RaiseEvent ProgressUpdate((m_BytesCopied / _ m_BytesToCopy) * 100) End Sub |
Concatenate Files With Manual Copy |
Express News India | Freelance ecommerce web development India