1 1 1 1 1 1 1 1 1 1 Rating 4.20 (5 Votes)

Problemstellung:

Wie kann ich mit API eine Datei kopieren?  

Für 32Bit und 64Bit Office Versionen

    'bFailIfExists gibt es folgende Möglichkeiten:
    '"0": eine vorhandene Datei soll überschrieben werden
    '"1": eine vorhandene Datei soll nicht überschrieben werden
    'Die Funktion liefert zwei verschiedene Rückgabewerte:
    '"0": Vorgang erfolgreich
    '"1": Vorgang nicht erfolgreich

#If VBA7 Then
    'Code für 32 bit und 64 bit Office VBA 7
     #If Win64 Then
        Declare PtrSafe Function CopyFile Lib "kernel32" Alias "CopyFileA" _
            (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, _
            ByVal bFailIfExists As Long) As Long
     #Else
        Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" _
            (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, _
            ByVal bFailIfExists As Long) As Long
     #End If
#Else
    Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" _
        (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, _
        ByVal bFailIfExists As Long) As Long
#End If

Public Function API_CopyFile(strSourceFile As String, strTargetFile As String, _
                             Optional lngFileExist As Long = 0) As Long
'*******************************************
'Name:      API_CopyFile (Function)
'Purpose:   Datei kopieren
'Author:    Tommyk
'Date:      März 11, 2004, 04:04:19
'Inputs:    strSourceFile=Pfad und Name der Quelldatei, strTargetFile=Pfad und Name der Zieldatei
'           lngFileExist=0 eine vorhandene Datei soll überschrieben werden
'           lngFileExist=1 eine vorhandene Datei soll nicht überschrieben werden
'Output:
'*******************************************
   API_CopyFile = CopyFile(strSourceFile, strTargetFile, lngFileExist)
   If API_CopyFile = 0 Then
      MsgBox "Kopieren der Datei " & strSourceFile & " fehlgeschlagen", vbInformation, "Fehler"
      Exit Function
   Else
      MsgBox "Die Datei wurde erfolgreich kopiert.", vbInformation, "Erfolgreich"
   End If
End Function

Aufruf:

API_CopyFile "C:\Test\Test2\Crypter.dll", "H:\Test\Crypter.dll"

würde die Datei "C:\Test\Test2\Crypter.dll" nach "H:\Test\Crypter.dll" kopieren.
Ist Datei bereits vorhanden, wird sie überschrieben

API_CopyFile "C:\Test\Test2\Crypter.dll", "H:\Test\Crypter.dll", 1

würde die Datei "C:\Test\Test2\Crypter.dll" nach "H:\Test\Crypter.dll" kopieren.
Ist Datei bereits vorhanden, wird sie nicht überschrieben.

 

Ähnliche Artikel