VB icon

Alternative FileCopy - Use to copy opened access databases or to copy a file and make a prog. bar

Email
Submitted on: 1/17/2015 8:58:00 PM
By: Matheus Moreira (from psc cd)  
Level: Intermediate
User Rating: By 16 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 798
 
     I made this code because I need to copy an access database with the file open (in use). But, visual basic FileCopy method and windows apis for this pourpose fails in this case with the "File Access Error". So, I made this function that copy the file in blocks. You can alter the block size so the copy can be faster or slower. Well, thats it. I hope that this code can be useful to anyone! Ah, the error handle was generated with Ax-Tools CodeSmart 2001, an excelent Add-In for any visual basic programmer! Recommended! :) www.axtools.com
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Alternative FileCopy - Use to copy opened access databases or to copy a file and make a prog. bar
' Description:I made this code because I need to copy an access database with the file open (in use). But, visual basic FileCopy method and windows apis for this pourpose fails in this case with the "File Access Error". So, I made this function that copy the file in blocks. You can alter the block size so the copy can be faster or slower. 
Well, thats it. I hope that this code can be useful to anyone!
Ah, the error handle was generated with Ax-Tools CodeSmart 2001, an excelent Add-In for any visual basic programmer! Recommended! :) www.axtools.com
' By: Matheus Moreira (from psc cd)
'**************************************

Public Function CopyFile(Source As String, Destiny As String, Optional BlockSize As Long = 32765) As Boolean
'<EhHeader>
On Error GoTo CopyFile_Err
'</EhHeader>
Dim Pos As Long
Dim posicao As Long
Dim pbyte As String
Dim buffer As Long
Dim Exist As String
Dim LenSource As Long
Dim FFSource As Integer, FFDestiny As Integer
 
100 buffer = BlockSize
102 posicao = 1
104 Exist = ""
106 Exist = Dir$(Destiny)
108 If Exist <> "" Then Kill Destiny
110 FFSource = FreeFile
112 Open Source For Binary As #FFSource
114 FFDestiny = FreeFile
116 Open Destiny For Binary As #FFDestiny
118 LenSource = LOF(FFSource)
120 For Pos = 1 To LenSource Step buffer
122 If Pos + buffer > LenSource Then buffer = (LenSource - Pos) + 1
124 pbyte = Space$(buffer)
126 Get #FFSource, Pos, pbyte
128 Put #FFDestiny, posicao, pbyte
130 posicao = posicao + buffer
'132 RaiseEvent Progress(Round((((Pos / 100) * 100) / (LenSource / 100)), 2))
'134 DoEvents
Next
136 Close #FFSource
138 Close #FFDestiny
'140 RaiseEvent CopyComplete
'<EhFooter>
Exit Function
CopyFile_Err:
MsgBox "Um erro inesperado ocorreu!" & vbCrLf & _
"Por favor anote ou copie (Pressionando a tecla 'Print-Screen' e depois CTRL+V no PAINT) os dados abaixo:" & vbCrLf & _
"No Erro: " & Err.Number & vbCrLf & _
"Local: Project1.Form1.CopyFile " & vbCrLf & _
"Linha: " & Erl & vbCrLf & vbCrLf & _
"Descrição: " & Err.Description & vbCrLf & vbCrLf & _
"Operação Cancelada!", vbCritical, "Erro!"
Screen.MousePointer = vbDefault
Resume CopyFile_Sai
CopyFile_Sai:
Exit Function
'</EhFooter>
End Function


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.