Correct usage of FileSystemObject- Copy Folder

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,835
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Having a bit of trouble with this.
I want to copy a Folder including it's contents and any subfolders (and their contents)
I have set a reference to Microsoft Scripting RunTime.
VBA Code:
Dim fs As New FileSystemObject
Set fs = CreateObject("Scripting.FileSystemObject")
for i =1 to 10
MkDir "M:\Test\Volume " & i
Call fs.CopyFolder("D:\Data*", "M:\Test\Volume " & i & "\")
stop
Next
I reach Stop without error and the Folders are created (and subfolders filled) but no files are copied to the top folder "M:\Test\Volume 1"
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I reach Stop without error and the Folders are created (and subfolders filled) but no files are copied to the top folder "M:\Test\Volume 1"
I think CopyFolder is working as expected. You won't get files in "M:\Test\Volume 1", only folders matching Data*, and subfolders and files within the Data* folders.
 
Upvote 0
Okay. How could the code be modified to get all the files ?
 
Upvote 0

Another way is a VBA Shell call just using the easy XCopy DOS command …​
 
Upvote 0
John_w - Those files in "M:\Test\Volume 1", which you correctly said I wouldn't get.
Dos Xcopy? Been a while since I used that. I'd rather do it without Shelling if possible.
 
Upvote 0
John_w - Those files in "M:\Test\Volume 1", which you correctly said I wouldn't get.
I meant which files in the source folders - do you mean the files in "D:\"?

At the end, "M:\Test\Volume 1\" should contain all the folders matching "D:\Data*", e.g. "Data1", "Data2", etc., along with all their files and subfolders.

Note - I get a "Path/File access error" on the MkDir line if the destination folder already exists, fixed with this change:
VBA Code:
Public Sub Copy_Folders()

    Dim fs As FileSystemObject, i As Long, destFolder As String
    
    Set fs = New Scripting.FileSystemObject
    For i = 1 To 10
        destFolder = "M:\Test\Volume " & i & "\"
        If Dir(destFolder, vbDirectory) = vbNullString Then MkDir destFolder
        fs.CopyFolder "D:\Data*", destFolder
    Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,558
Latest member
aivin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top