Home > How-To Library > File/Folders Handling

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

**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * ****************************************************************
'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

If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.