Macro to copy subdirectories...

Puma

Board Regular
Joined
Mar 19, 2002
Messages
72
I have a macro that runs off into a directory structure and copies files (always in the form "v:\A\B\C" where A,B,C vary) to a new directory structure somewhere else that the macro creates based on a list it is supplied. This works fine.

However, I've now discovered that sometimes there are subdirectories in C which need to be copied as well...

How do i adapt (or rewrite if that's easier...) this code to target folders not files (or both at the same time for effeciency i suppose)?

Application.FileSearch.LookIn = "V:\" & A & "\" & B & "\" & C
Application.FileSearch.Filename = "*.*"
If Application.FileSearch.Execute Then

.CopyFile ("V:\" & A & "\" & B & "\" & C & "\" & "*.*") _
, ("V:\" & Name & "\" & Number & "\" & A & "\" & B & "\" & C)

End If

Cheers for any help.

Dave
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I don't know if this will help, as i don't really understand the details of the "list supplied" you speak of. But here is some vba that will copy all files and subdirectories starting at a specified directory and copy those files & subfolders to another directory.

Here is the code :
Code:
' Directories shouldn't have "\" at end of them when passed to recursive
Public Sub gonow()
   Call Recursive("C:\one", _
                  "C:\two")
   MsgBox "Done."
End Sub


Public Sub Recursive(source_path As String, dest_path As String)
   Dim counter As Long
   Dim src_subdir, dest_subdir, file, fs

   Set fs = CreateObject("Scripting.FileSystemObject")
   For Each src_subdir In fs.getfolder(source_path).subfolders
      Set dest_subdir = fs.CreateFolder(dest_path & "\" & src_subdir.Name)
      Call Recursive(src_subdir.Path, dest_subdir.Path)
   Next
   
   For Each file In fs.getfolder(source_path).Files
      Application.StatusBar = "Copying File <" & file.Path & "> to specified destination."
      FileCopy file.Path, dest_path & "\" & file.Name
   Next
   
End Sub

In this example i've passed "C:\one" and "C:\two". "C:\one" can contain and number of files and subfolders and then any number of files and subfolders within the subfolders, and so on, and so on. The subfolders will be created in the destination folder "C:\two" and the files will be copied over.

You can modify to code to only copy certain files or to ignore certain filetypes, or whatever you want.

adampease
 
Upvote 0
Didn't explain it very well... sorry!

Simply, if a folder exists on a network drive, i need to copy all it's contents to somewhere else. ie. emulate the "edit > select all, copy, paste" functions avaliable in a windows explorer window. The macro does a whole bunch of other things with the files but, that bit works.

Getting the files to copy was easy:

Application.FileSearch.LookIn = "V:\DIR"
Application.FileSearch.Filename = "*.*"
If Application.FileSearch.Execute Then
.CopyFile ("V:\DIR & "\" & "*.*") _
, ("C:\DIR")

But, i can't get the CopyFolder command to work with wildcards...

If .FolderExists("V:\DIR & "\" & "*") Then
.CopyFolder ("V:\DIR & "\" & "*") _
, ("C:\NEW")

the "*" bit doesn't work... was hoping there was a different way to reference folder names in a search like this. If not, I see where you were going with your code and i'll have to adapt that to my situation!

Thanks for helping!

Dave
 
Upvote 0
Ah ha!!!!

Got it working (ish but, at least the tricky bit works. all i've got to sort now is the easy bits round the edge!!).

You're a genius mate, thank you very much!

Dave :pray:
 
Upvote 0

Forum statistics

Threads
1,215,097
Messages
6,123,076
Members
449,094
Latest member
mystic19

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