CodeItBetter Programming Another VB Programming Blog

How to Copy the files to another directory (in three ways)

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'File/Folder Handling - How to Copy the files to another directory (in three ways)
'Using FileCopy:

Public Sub CopyFileUsingFileCopy(ByVal sSourceFile As String, ByVal sDestinationFile As String)
    'All file operations should be protected against errors.
    'None of these functions works on open files.
    On Error Resume Next
    'By default it will overwrite the existting file
    FileCopy sSourceFile, sDestinationFile
End Sub
 
'Using FSO:

Public Sub CopyFileUsingFSO(ByVal sSourceFile As String, ByVal sDestinationFile As String, _
    Optional ByVal bOverWrite As Boolean = True)
    'Set a reference to "Microsoft Scripting Runtime"
    
    ' All file operations should be protected against errors.
    ' None of these functions works on open files.
    On Error Resume Next
    ' Make a copy of a file--note that you can change the name during the copy
    ' and that you can omit the filename portion of the target file.
    Dim FSO As Scripting.FileSystemObject
    Set FSO = New Scripting.FileSystemObject
    FSO.CopyFile sSourceFile, sDestinationFile, bOverWrite
    Set FSO = Nothing
End Sub
 
'Using API:

Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" _
    (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, _
    ByVal bFailIfExists As Long) As Long
 
Public Sub CopyFileUsingAPI(ByVal sSourceFile As String, ByVal sDestinationFile As String, _
    Optional ByVal bFailIfExists As Boolean = False)
    Call CopyFile(sSourceFile, sDestinationFile, bFailIfExists)
End Sub
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.