Does anyone know how to copy subfolders from one place to another in VB?. I have a Template folder on a network share I want to use to set up the base folder structure on newly created job folders, however, I can't seem to get any copy methods to take just the subfolders and not the parent.

The base (from) template folder is structrued as;

\\server\
..\_templates\
....\input\
......\communications\
......\data\
.....\output\
......\communications\
......\data\
and so on..

I have:

Public Sub CopyFilesFolder2Folder(Foldername As String)

    Dim fso
    
    sfol = "\\server\main_jobs\_Templates\" ' source folder path
    dfol = Foldername ' destination folder path

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set Folder = fso.GetFolder(sfol)

    fso.GetFolder(Folder).Copy dfol
    Set fso = Nothing

This copy's the folder just fine, but, carrys the parent (_Templates) too, so the resulting desitnation is \\newserver\newjob\_Templates\folder1, 2, 3, ect. wher I want it to be \\newserver\newjob\folder1, 2, 3, ect.

Any ideas?, I'm stumped on this.

Recommended Answers

All 12 Replies

Change the line 7 to:
dfol = "..\" & Foldername

Hope this helps

No, this fix throws out a "Folder not found" error. In hovering, it appears as though the source folder in "fso.GetFolder(Folder).Copy dfol" is missing the trailing slash, making it a file, not a folder - causing the error. Still trying to figure it out, but thanks..

If I understood right, the problem is that the destination folder does not exists?
Then you should use fso.CreateFolder before copying.

Anyway, instead of using fso.GetFolder(Folder).Copy .. I would prefer the fso.CopyFolder( fromFolder , toFolder, Overwrite)

Hope this helps

The folder exists, I create it using "CreateNewFolder", the problem is that it's nested in a certain path structure (Client\Year\JobNumber_name\). The Template folder is what needs to go into the nested Job folder (actually, just the folders inside it). We need to create a consistent folder structure that's common for every job so there is no question as to where anything is for turnover or coverage. "Data" is always in the '"Client\Year\JobNumber_name\Input\Data" folder, Reports are always in the "Client\Year\JobNumber_name\Output\Reports" folder. This is the idea.

As the Project manager creats a new job entry, Access will automatically create that folder structure by using the Index to the Client list to determine the root client folder name, ect.

To copy the files and folders inside _templates from \\Server\mainjobs\_templates to the new destination directory you need to iterate through the \\Server\mainjobs\_templates subfolders.

After

Set SourceRootFolder = fso.GetFolder(sfol)

you can loop by subfolder as (untested)

For Each Folder In SourceRootFolder.SubFolders
    fso.GetFolder(Folder).Copy dfol & "\" & Folder.Name
Next

Also if you need to copy the files from the root folder then (also untested)

For Each ExistingFile In SourceRootFolder.Files
    fso.GetFile(ExistingFile).Copy dfol & "\" & ExistingFile.Name
Next

Hope this helps

Let me give it a shot, but it looks good.. :)

Still erroring out;

dim SourceRootFolder As FileSystemObject

    sfol = "\\SERVER\Maijobs\_Templates\" 
    dfol = Foldername
    
    Set SourceRootFolder = fso.GetFolder(sfol)
    
    For Each Folder In SourceRootFolder.subFolders
        fso.GetFolder(Folder).Copy dfol & "\" & Folder.Name
    Next

Throws a ".subFolders Method or Data Member not found" error.

I feel sooooooo close!.. ugh!

Maybe an spelling problem. The property is SubFolders (starting uppercase see here)

:)

No, "SubFolders" doesn't appear on the list of available methods. Perhaps I should be dimesioning SourceRootFolder as something other than a FileSystemObject?

The default dim is as object, like in the example on the page I recommended to read:

Function ShowFolderList(folderspec)
   Dim fso, f, f1, s, sf
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFolder(folderspec)
   Set sf = f.SubFolders
   For Each f1 in sf
      s = s & f1.name 
      s = s & "<BR>"
   Next
   ShowFolderList = s
End Function

Hope this helps

Actually, I'm getting a '454 Object Required' error on "Set SourceRootFolder = fso.GetFolder(sfol)", thats why I dimme'd 'SourceRootFolder' as a FileSystemObject.

Got it figured out. I id reference the page you mentioned, and actually ran the code. When VBA was erroring out, it indicated the variable "SourceRootFolder" was the issue, however, I needed to create fso first. after adding "Set fso = CreateObject("Scripting.FileSystemObject")", it worked like a charm!

THANKS!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.