Solved

APICopyFIle does nothing

Posted on 2006-10-19
6
614 Views
Last Modified: 2007-12-19
From Access 2002 (XP) VB code.

I have:

"
Option Compare Database
Option Explicit

Declare Function apiCopyFile Lib "kernel32" Alias "CopyFileA" _
        (ByVal lpExistingFileName As String, _
         ByVal lpNewFileName As String, _
         ByVal bFailIfExists As Long) As Long
         
Sub CopyFile(SourceFile As String, DestFile As String)
'---------------------------------------------------------------
' PURPOSE: Copy a file on disk from one location to another.
' ACCEPTS: The name of the source file and destination file.
' RETURNS: Nothing
'---------------------------------------------------------------
    Dim result As Long
    If Dir(SourceFile) = "" Then
        MsgBox Chr(34) & SourceFile & Chr(34) & _
               " is not valid file name."
    Else
        result = apiCopyFile(SourceFile, DestFile, False)
    End If
     
End Sub
"

The APICopyFIle returns 0 but does not copy the file(s) across! And, before you ask, the original file exists in the path I give it. So does the target floder.

Example of use:

"
        CopyFile COU30 & "Cg3 US " & username & ".mde", COU3
"

where COU30 is string "C:\Program Files\Fred 3\"
and COU3 is string "C:\Program Files\Fred 3.1\"
and username is "example3"

Any ideas, however insultingly obvious they should have been to me...

Many thanks.
0
Comment
Question by:Peborgh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 17767049
you can just use the Access command FileCopy


    FileCopy SourcePath, DestPath
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17767057
Perhaps destfile is not writable, permissions not there?



As a alternative, have u tried FileCopy? builtin func, no api required


Sub CopyFile2(SourceFile As String, DestFile As String)

    Dim result As Long

    On Error Resume Next

    If Dir(SourceFile) = "" Then
        MsgBox Chr(34) & SourceFile & Chr(34) & _
               " is not valid file name."
    Else
        Err.Clear
        If DIr$(DestFile) <> "" Then kill DestFile
        If Err.Number <> 0 Then
             msgbox "Crap, I cant delete the damn destination file." & vbcrlf & err.description
        Else
            FileCopy SourceFile, DestFile
            If Err.Number <> 0 Then
                msgbox "Not having a good day as the file copy has failed.." & vbcrlf & err.description
            End if
        End if
    End If
     
End Sub

0
 

Author Comment

by:Peborgh
ID: 17768151
I was using my CopyFile because FileCopy died when the destination file already existed and, I think, because the destination had to be afile and could not be a folder. I am not sure any more, sorry. Or maybe it was that I could not copy a wildcarded bucnh of files at a time...

If you can aasure me that FileCopy will do these things I require, I will repent and return to the fold!

Many thanks,

peter
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 65

Accepted Solution

by:
rockiroads earned 250 total points
ID: 17768622
Ok, so u want the option to copy to folders

Lets see if this works


Sub CopyFile2(SourceFile As String, DestFile As String)

    Dim result As Long

    On Error Resume Next

    If Dir(SourceFile) = "" Then
        MsgBox Chr(34) & SourceFile & Chr(34) & _
               " is not valid file name."
    Else
        Err.Clear
        'Check if DestFile is a folder

        If Dir$(DestFile,vbDirectory) <> "" Then
            If Right$(DestFile,1) <> "\" Then DestFile = DestFile & "\"
            DestFile = DestFile & Dir$(SourceFile)
        End if

        If DIr$(DestFile) <> "" Then kill DestFile
        If Err.Number <> 0 Then
             msgbox "Crap, I cant delete the damn destination file." & vbcrlf & err.description
        Else
            FileCopy SourceFile, DestFile
            If Err.Number <> 0 Then
                msgbox "Not having a good day as the file copy has failed.." & vbcrlf & err.description
            End if
        End if
    End If
     
End Sub



dont do wildcards though
0
 

Author Comment

by:Peborgh
ID: 17773664
All, thank you for all your trouble, especially Bumpy Way.

In the end, the solution I wnet for that does all I want, is to use FileSystemObject.CopyFile. Points to Crooked Mile for trying so hard...
peter
0
 

Author Comment

by:Peborgh
ID: 17773668
... and humourous error messages.
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question