Copying files from one folder to another using vba

后端 未结 3 537
一个人的身影
一个人的身影 2021-01-26 18:17

There are some similar posts about this topic, I know. However, I have a code which is different than all codes I have seen here (when talking about this subject).

The e

3条回答
  •  忘了有多久
    2021-01-26 19:13

    To copy all files and sub-folders recursively, use the following code:

    Public Sub CopyDirectory(ByVal source As String, ByVal destination As String)
        Dim fso, file, folder As Object
        Set fso = CreateObject("Scripting.FileSystemObject")
        'Delete existing folder
        If fso.FolderExists(destination) Then fso.DeleteFolder destination, True
        fso.CreateFolder destination
        For Each file in fso.GetFolder(source).files
            fso.CopyFile file.Path, destination & "\" & file.Name
        Next file
        For Each folder in fso.GetFolder(source).SubFolders
            CopyDirectory folder.Path, destination & "\" & folder.Name
        Next folder
    End Sub
    

    Use as following:

    CopyFile "C:\Path To Source", "C:\Path to destination"
    

    Note that the paths should not include a trailing directory separator (\).

提交回复
热议问题