How to Copy Folder from one directory to another directory (in Two ways)
Posted on June 24, 2011
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 | 'File/Folders Handling - How to Copy Folder from one directory to another directory (in Two ways) 'Using FSO: Public Sub CopyFolderUsingFSO(ByVal sSourceFolder As String, ByVal sDestinationFolder 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 Dim FSO As Scripting.FileSystemObject Set FSO = New Scripting.FileSystemObject FSO.CopyFolder sSourceFolder, sDestinationFolder, bOverWrite Set FSO = Nothing End Sub 'Using XCopy: Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal lpPath As String) As Long Public Sub CopyFolderUsingXcopy(ByVal sSourceFolder As String, ByVal sDestinationFolder As String, _ Optional ByVal bOverWrite As Boolean = True, Optional ByVal bFailIfError = False) Dim sCommand As String Call MakeSureDirectoryPathExists(sDestinationFolder) sCommand = "Cmd.exe /C xcopy " & ChrW$(34) & sSourceFolder & ChrW$(34) & " " & ChrW$(34) & _ sDestinationFolder & ChrW$(34) & " /E /Q" If bOverWrite Then sCommand = sCommand & " /Y" If Not bFailIfError Then sCommand = sCommand & " /C" Shell sCommand End Sub |