Hi there...

I have some questions on how to optimize copying data.. here is may code...


Dim aUnicode() As Byte
Dim g1 As Integer
Dim g2 As Integer
Dim fname As String

Function Binary_copy(sourcef As String, destf As String) As Long
Dim totalS, chunksize As Long
Dim wrtn As Long, pse, st, et
wrtn = 0
totalS = CreateObject("Scripting.FileSystemObject").GetFile(sourcef).Size

If Int(totalS / 200) > 3000000 Then chunksize = 3000000 Else chunksize = Int(totalS / 200)

g1 = FreeFile: Open sourcef For Binary As #g1
g2 = FreeFile: Open destf For Binary As #g2
st = Timer

Do Until EOF(g1)
If wrtn + chunksize > totalS Then chunksize = totalS - wrtn
If chunksize = 0 Then chunksize = 1
ReDim aUnicode(1 To chunksize)
Get #g1, , aUnicode
Put #g2, , aUnicode
wrtn = wrtn + chunksize
Label1.Caption = Round(wrtn / 1000000, 2) & " MB out of " & Round(totalS / 1000000, 2) & " MB copied: " & Round(wrtn / totalS, 2) * 100 & "% done..."
pse = Timer + 0.001
et = Timer
If (Int(((et - st) * totalS) / wrtn)) - (Int(et - st)) > 60 Then Label2.Caption = Round((Int(((et - st) * totalS) / wrtn) / 60) - (Int(et - st) / 60), 2) & " mins remaining." Else Label2.Caption = Round((Int(((et - st) * totalS) / wrtn)) - (Int(et - st))) & " seconds remaining."
Do Until Timer > pse
DoEvents
Loop
Loop

Close #g1
Close #g2

End Function

Private Sub Command1_Click()
Dim s As String, d As String, fname
fname = "DB_backup.bak"
s = "\\right_eight\F\" & fname
d = "E:\" & fname
Binary_copy s, d

End Sub

Can you please correct me some errors to optimize the copy process thanks...
I want to know too is it possible to multipy the process to make it much faster
example: in the do while loop... can i make a copy process that copies bytes from start of the file and the other process will read start from the end of the file to make it twice faster.

examples: a file has 10bytes the other process will copy the 1-5bytes and the other is 6-10bites simultenously..


sorry for my english.. any help is much appreciated..

thanks...

Recommended Answers

All 4 Replies

xirosen, the code below will shorten your code a lot, just manipulate it to suit your needs. I have also attached a sample of copying using winsock control. The speed of copying however is not in your code, but in your system hardware i.e. RAM, CPU, LAN speed of 10 mbps to 100 mbps, your internet connection etc. -

Open "file1" For Binary As #1
Open "file2" For Binary As #2

whole& = LOF(1) \ 10000         'number of whole 10,000 byte chunks
        part& = LOF(1) Mod 10000        'remaining bytes at end of file
        buffer1$ = String$(10000, 0)
        buffer2$ = String$(10000, 0)
        start& = 1
        For x& = 1 To whole&            'this for-next loop will get 10,000
        Get #1, start&, buffer1$      'byte chunks at a time.
        Get #2, start&, buffer2$
        If buffer1$ <> buffer2$ Then
        	Exit For
        End If

        start& = start& + 10000
        Next
        buffer1$ = String$(part&, 0)
        buffer2$ = String$(part&, 0)
        Get #1, start&, buffer1$        'get the remaining bytes at the end
        Get #2, start&, buffer2$        'get the remaining bytes at the end
        If buffer1$ <> buffer2$ Then 'whatever code here
        End If

Sorry, the attachment -

Thanks andreret...

Only a pleasure. If this solved your problem, please mark as solved, thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.