[AccessD] Copy a file

Jurgen Welz jwelz at hotmail.com
Thu Feb 23 16:57:35 CST 2006


Bill:

The API procedure you quote includes an 'Overwrite' parameter:

ByVal bFailIfExists As Long


Your example is set to overwrite a file existing at the destination by 
setting this parameter false.  Presumably a read only file shouldn't 
overwrite, but if it does, the call to Kill is unnecessary if it is not 
readonly or you've removed the readonly attribute.

I occasionally find it helpful to open the file exclusive, or attempt to 
rename the target file to ensure no one is working with it when I overwrite 
an existing file.  In some situations this is a mandatory precaution.

I usually prefer to Recycle a file existing at the target prior to 
overwrite:

Public Function Recycle(strFilePathAndFileName As String) As Boolean
    Dim Killed As FileOpStruct

Private Type FileOpStruct
    hwnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAborted As Boolean
    hNameMaps As Long
    sProgressTitle As String
End Type

Public Function Recycle(strFilePathAndFileName As String) As Boolean
    Dim Killed As FileOpStruct

    If Len(Dir(strFilePathAndFileName)) Then
        With Killed
            .wFunc = 3
            .pFrom = strFilePathAndFileName & vbNullChar
            .fFlags = 80 '64 is adequate
        End With
        Recycle = SHFileOperationA(Killed) = 0
    End If
End Function

In the case of server files, my environment experience is that there is no 
ready access to the recycle bin.  In that case, I have found it beneficial 
to move the file to a local drive and recycle there.  You'd be surprised how 
many times that preempted a call to helpdesk for a restore from tape.  That 
was in the days before Terminal Server.  Nowadays, I find it helpful to 
prepend a date prefix to the replaced or 'overwritten' file name.

Depending on the circumstances, it is frequently useful to check for the 
existence of a file at the destination and prompt the user.  Based on this 
input, you may set the 'overwrite' parameter in the caller.

Ciao
Jürgen Welz
Edmonton, Alberta
jwelz at hotmail.com





>From: "Bill Patten" <bill_Patten at earthlink.net>
>
>Here is routine that I use.
>Declare Function apiCopyFile Lib "kernel32" Alias "CopyFileA" _
>     (ByVal lpExistingFileName As String, _
>   ByVal lpNewFileName As String, _
>     ByVal bFailIfExists As Long) As Long
>   Declare Function SetFileAttributes Lib "kernel32" Alias
>"SetFileAttributesA" _
>     (ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long
>
>Function BPCopyFile(SourceFile As String, DestFile As String, Optional
>intReadOnly As Integer) As Boolean
>On Error GoTo Err_BPCopyFile
>'----------------------------------------------------------------------------
>' Purpose: Copy a file on disk from one location to another and on request
>change to or from readonly
>'   BPcopyfile "c:\dog.oub, c:\cat.mdb , optional 1 = readonly 2 = not
>readonly
>'   though this function will copy any files (open ones too) it was 
>designed
>to copy a
>'   file from a CD, rename it and then make sure it is not read only
>' By: Bill Patten, BPCS
>' Date:     9/16/2003 8:41:58 PM
>' Mods:
>'----------------------------------------------------------------------------
>   Const FILE_ATTRIBUTE_NORMAL = 128
>   Const FILE_ATTRIBUTE_READONLY = 1
>   Dim Result As Long
>   BPCopyFile = False 'Assume it wont work
>   If Dir(DestFile) <> "" Then ' desination file exist so kill it
>     'Just in case it is readonly and would not delete
>     Result = SetFileAttributes(DestFile, FILE_ATTRIBUTE_NORMAL)
>     Kill (DestFile)
>   End If
>
>   If Dir(SourceFile) = "" Then
>     MsgBox Chr(34) & SourceFile & Chr(34) & _
>       " is not valid file name."
>     Exit Function
>   Else
>     Result = apiCopyFile(SourceFile, DestFile, False)
>   End If
>   If Not IsNull(intReadOnly) Then
>     Select Case intReadOnly
>       Case 1  'Make file read only
>         Result = SetFileAttributes(DestFile, FILE_ATTRIBUTE_READONLY)
>       Case 2  'remove readonly
>         Result = SetFileAttributes(DestFile, FILE_ATTRIBUTE_NORMAL)
>     End Select
>   End If
>
>BPCopyFile = True
>Exit_BPCopyFile:
>   Exit Function
>
>Err_BPCopyFile:
>   MsgBox "Error " & Err.Number & ": " & Err.Description & " In Sub 
>CopyFile
>In Module modFileCopy" & vbCrLf
>   Resume Exit_BPCopyFile
>
>End Function
>
>
>
>
>----- Original Message -----
>From: "Reuben Cummings" <reuben at gfconsultants.com>
>To: "AccessD" <accessd at databaseadvisors.com>
>Sent: Thursday, February 23, 2006 10:12 AM
>Subject: [AccessD] Copy a file
>
>
>Anyone have a system setup to copy/move files?
>
>I want to give a client a small app that she can open, click a button and 
>it
>will copy the specified file to another specified folder elsewhere on the
>network.
>
>Reuben Cummings
>GFC, LLC
>812.523.1017





More information about the AccessD mailing list