devxlogo

Copy a directory tree

Copy a directory tree

Creating a VB routine that copies a whole directory tree can be a time-consuming job, but fortunately you don’t have to do it. In fact, you can quickly create an entire folder, including all its subfolders, using the CopyFolder method of the FileSystemObject object, that is exposed by the Microsoft Scripting Runtime type library.

' copy the entire contents of the C:DOCS directory into the' C:BACKUPDOCS directory, overriding existing files.'' This code assumes that you added a reference to the ' Microsoft Scripting Runtime type libraryDim fso As New Scripting.FileSystemObjectfso.CopyFolder "c:docs", "c:ackupdocs", True

If you pass False to the 3rd argument (or omit it), the CopyFolder method doesn’t copy files that already exist in the destination folder.

devx-admin

Share the Post: