I modified the sub below from source I found on the net to copies files using a buffer and a byte array. It seems to work but the newly copied files have an extra byte and I don't know where it is coming from?
Any ideas on how to improve this code would also be appreciated....
Thanks..
'======================
Public Sub copyBin(source As String, dest As String)
Dim readChunkSize As Long
Dim bufferSize As Long, tempSize As Long
Dim infil As Long
Dim outfil As Long
Dim r() As Byte, numBytes As Long
Dim EndOfCTime As Single
Dim FCStTime As Single
Dim buffer() As Byte
Dim i As Long, upper As Long 'used as indices
Dim CurPosOn As Long
Dim Totalto As Long
Dim temp As Long
Dim binCol As New Collection
Dim myByte As clsByteArray
Dim tempByte As clsByteArray
If myCacheSize = 0 Then
readChunkSize = 32767
Else
readChunkSize = myCacheSize
End If
bufferSize = readChunkSize * 2
If Dir$(source) = "" Then
mySucessfull = False
Exit Sub
End If
'If Dir(Destination) <> "" Then
' Kill Destination
'End If
FCStTime = Timer
infil = FreeFile
Open source For Binary As #infil
outfil = FreeFile
Open dest For Binary As #outfil
If readChunkSize > LOF(infil) Then
Get #infil, , r
Put #outfil, , r
GoTo CLoseFiles
End If
upper = LOF(infil) \ readChunkSize
temp = LOF(infil) Mod readChunkSize
If temp <> 0 Then
upper = upper + 1 ' as the remainder will never be greater then the denominator(?)
End If
numBytes = 0
For i = 1 To upper
ReDim r(readChunkSize)
Get #infil, , r
numBytes = numBytes + UBound(r)
'now we need to put r into buffer
Set myByte = New clsByteArray
myByte.byteArray = r
binCol.Add myByte
If numBytes >= bufferSize Then
For Each tempByte In binCol
Put #outfil, , tempByte.byteArray
Next 'tempbyte
numBytes = 0
Set binCol = New Collection
End If
CurPosOn = Loc(outfil) / 100
Totalto = LOF(infil) / 100
myPercentReady = (CurPosOn * 100) / Totalto
DoEvents
If LOF(infil) - Loc(infil) < readChunkSize Then
readChunkSize = LOF(infil) - Loc(infil)
End If
Next 'i
If binCol.Count > 0 Then
For Each tempByte In binCol
Put #outfil, , tempByte.byteArray
Next 'tempbyte
End If
CLoseFiles:
Close #infil, #outfil
mySucessfull = True
EndOfCTime = Timer
mySecondsTaken = EndOfCTime - FCStTime
Set binCol = Nothing
Set myByte = Nothing
Set tempByte = Nothing
End Sub
'======================
'clsByteArray
Option Explicit
Private myByte() As Byte 'holds the byte array
Public Property Get byteArray() As Byte()
byteArray = myByte
End Property
Public Property Let byteArray(ByRef byteA() As Byte)
myByte = byteA
End Property
Troy Williams B.Eng.
fenris@hotmail.com
Any ideas on how to improve this code would also be appreciated....
Thanks..
'======================
Public Sub copyBin(source As String, dest As String)
Dim readChunkSize As Long
Dim bufferSize As Long, tempSize As Long
Dim infil As Long
Dim outfil As Long
Dim r() As Byte, numBytes As Long
Dim EndOfCTime As Single
Dim FCStTime As Single
Dim buffer() As Byte
Dim i As Long, upper As Long 'used as indices
Dim CurPosOn As Long
Dim Totalto As Long
Dim temp As Long
Dim binCol As New Collection
Dim myByte As clsByteArray
Dim tempByte As clsByteArray
If myCacheSize = 0 Then
readChunkSize = 32767
Else
readChunkSize = myCacheSize
End If
bufferSize = readChunkSize * 2
If Dir$(source) = "" Then
mySucessfull = False
Exit Sub
End If
'If Dir(Destination) <> "" Then
' Kill Destination
'End If
FCStTime = Timer
infil = FreeFile
Open source For Binary As #infil
outfil = FreeFile
Open dest For Binary As #outfil
If readChunkSize > LOF(infil) Then
Get #infil, , r
Put #outfil, , r
GoTo CLoseFiles
End If
upper = LOF(infil) \ readChunkSize
temp = LOF(infil) Mod readChunkSize
If temp <> 0 Then
upper = upper + 1 ' as the remainder will never be greater then the denominator(?)
End If
numBytes = 0
For i = 1 To upper
ReDim r(readChunkSize)
Get #infil, , r
numBytes = numBytes + UBound(r)
'now we need to put r into buffer
Set myByte = New clsByteArray
myByte.byteArray = r
binCol.Add myByte
If numBytes >= bufferSize Then
For Each tempByte In binCol
Put #outfil, , tempByte.byteArray
Next 'tempbyte
numBytes = 0
Set binCol = New Collection
End If
CurPosOn = Loc(outfil) / 100
Totalto = LOF(infil) / 100
myPercentReady = (CurPosOn * 100) / Totalto
DoEvents
If LOF(infil) - Loc(infil) < readChunkSize Then
readChunkSize = LOF(infil) - Loc(infil)
End If
Next 'i
If binCol.Count > 0 Then
For Each tempByte In binCol
Put #outfil, , tempByte.byteArray
Next 'tempbyte
End If
CLoseFiles:
Close #infil, #outfil
mySucessfull = True
EndOfCTime = Timer
mySecondsTaken = EndOfCTime - FCStTime
Set binCol = Nothing
Set myByte = Nothing
Set tempByte = Nothing
End Sub
'======================
'clsByteArray
Option Explicit
Private myByte() As Byte 'holds the byte array
Public Property Get byteArray() As Byte()
byteArray = myByte
End Property
Public Property Let byteArray(ByRef byteA() As Byte)
myByte = byteA
End Property
Troy Williams B.Eng.
fenris@hotmail.com