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"
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)
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?
ReplyDeletei 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!
ReplyDelete