Sunday, July 20, 2008

Copying folders in VBA using File System Object

FolderCopy in VBA using File System Object

The following code copies the folder from one location to another. If there is any error while copying, it is logged in a text file.

Sub CopyFolders()

Dim fs, f, s

Dim copyerr As Boolean

copyerr = False

Source_Path = "d:\vbadud\"

Destn_Path = "d:\vbadud\dummy\"

folder_path = "d:\vbadud\folders.txt"

Open folder_path & "CopyErr.txt" For Output As 5

Open folder_path For Input As 1

Do While Not EOF(1)

sourcefolder = "d:\vbadud\"

destnfolder = "d:\vbadud\dummy\"

txtfile = ""

txtpath = "d:\vbadud\"

fs = CreateObject("Scripting.FileSystemObject")

If fs.folderexists(destnfolder) Then

Print #5, destnfolder & " already exists"

copyerr = True


fs.Copyfolder(sourcefolder, destnfolder, True)

End If

If copyerr = False Then Print #5, "No Errors while copying"


Close #1

Close #5

MsgBox("Folders Copied Succesfully. Refer " & txtpath & "CopyErr.txt for error details")

End Sub

The above code checks for the existence of the destination folder using fs.folderexists before copying. If a folder exists by the same name, it is not copied and it is logged in a text file. For writing text files please refer ( and

For copying files please refer


  1. Anonymous7:02 AM

    I wish to close an open folder and all it's contents, then copy it to another folder and finally delete the original. Can anyone help?

  2. Anonymous4:17 AM

    i have a similar code but the source file is the network drive and vba executes slowly, how can I make it fast? please help. Thanks!


Share on Facebook
Related Posts Plugin for WordPress, Blogger...
Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.