Showing posts with label VBA Copyfolder. Show all posts
Showing posts with label VBA Copyfolder. Show all posts

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

Else

fs.Copyfolder(sourcefolder, destnfolder, True)

End If

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

Loop

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 (http://vbadud.blogspot.com/2007/04/spacing-in-text-files.html and http://vbadud.blogspot.com/2007/04/run-time-error-55-file-already-open.html)

For copying files please refer http://vbadud.blogspot.com/2007/05/primitive-file-handling-functions.html

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.