Copyeng folders to another Directory (1 Viewer)

habiler

Registered User.
Local time
Tomorrow, 00:55
Joined
Aug 10, 2014
Messages
70
Good morning to you all,


I would like to copy the entire folder from K: to P: if the folder does not exist.
And then copy the existing folders from K to P.

I started with the non-existent files but it does not perform creation.

Code:
Option Compare Database
Option Explicit


Sub GetSubFolderNames()
Dim MyFSOK As FileSystemObject
'Dim MyFile As File
Dim MyFolderK As Folder
Dim MySubFolderK As Folder

Dim MyFSOP As FileSystemObject
Dim MyFolderP As Folder
Dim MySubFolderP As Folder

Dim FromPath

Set MyFSOP = New Scripting.FileSystemObject
Set MyFSOK = New Scripting.FileSystemObject

Set MyFolderK = MyFSOK.GetFolder("K:\DG")
Set MyFolderP = MyFSOP.GetFolder("C:\Pers1")

FromPath = MyFolderK

For Each MySubFolderK In MyFolderK.SubFolders
    If Not MyFSOP.FolderExists(MyFolderK) Then
MyFSOP.CreateFolder (MyFSOK)
Else
    
End If
Next MySubFolderK

End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 16:55
Joined
Oct 29, 2018
Messages
21,358
Hi. Just a guess, but it might be easier to use Shell scripting for this task.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 18:55
Joined
Feb 28, 2001
Messages
27,001
While I agree with theDBguy, let me point you to an article that might help you.

https://docs.microsoft.com/en-us/of...e/user-interface-help/filesystemobject-object

From this article, if you click on the various method names (in blue), you would see how to use these functions to create or copy files and folders.

In particular, you are trying to create folders, but the question is whether the target folder exists first. From what I can see of the routines, they are not very big on error reporting. Did you also want to copy the contents of the folders? I might look into the method .CopyFolders (which is in the referenced link, so I won't add another link to it.)
 

vba_php

Forum Troll
Local time
Today, 18:55
Joined
Oct 6, 2019
Messages
2,884
i don't know if you'd be interested in this, but you can also run command line executions from access code. for this, you would call xcopy with the appropriate switches to copy all the files in the subdirectories. xcopy is pretty fast, and prolly faster than the FileSystemObject you are using in ur code.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 18:55
Joined
Feb 28, 2001
Messages
27,001
Adam, that was theDBguy's suggestion. You do that with a shell script.

But you are quite right. A single XCopy command might be all that is required.
 

Users who are viewing this thread

Top Bottom