PDA

View Full Version : [SOLVED:] Create backup of folder with date



cwojtak
03-06-2020, 09:10 AM
Hey, looking to take a copy of a folder before I update it and save a copy of it under a different name. Used Ron De Bruins guide as a start but I'm getting a Runtime error 70, permission denied error on the second to last line, see my code below.



Sub BackupOwnershipFiles()'This example move the folder from FromPath to ToPath.

If MsgBox("Are you sure you'd like to create a back up of the ownership files?", vbYesNo) = vbNo Then
Exit Sub
Else
End If


Dim FSO As Object
Dim FromPath As String
Dim ToPath As String

yyyy = Year(Now)
mm = Format(Month(Now), "00")
dd = Format(Day(Now), "00")


FromPath = "S:\Regional Planners Product Ownership\Manager_Ownership_Files_" '<< Change
ToPath = "C:\Users\name\Documents\Manager Ownership Backups\" & mm & "_" & dd & "_" & yyyy '<< Change
'Note: It is not possible to use a folder that exist in ToPath


If Right(FromPath, 1) = "\" Then
FromPath = Left(FromPath, Len(FromPath) - 1)
End If


If Right(ToPath, 1) = "\" Then
ToPath = Left(ToPath, Len(ToPath) - 1)
End If


Set FSO = CreateObject("scripting.filesystemobject")


If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If


If FSO.FolderExists(ToPath) = True Then
MsgBox ToPath & " exist, not possible to move to a existing folder"
Exit Sub
End If


FSO.MoveFolder Source:=FromPath, Destination:=ToPath 'THIS IS WHERE THE ERROR IS
MsgBox "The folder is moved from " & FromPath & " to " & ToPath


End Sub

Artik
03-15-2020, 07:53 PM
Try copying the source folder to the destination folder, and then delete the source folder.

Artik

SamT
03-16-2020, 12:32 AM
Maybe

FSO.MoveFolder FromPath, ToPath

cwojtak
03-31-2020, 01:10 PM
Thank you for the input! I tracked it down to a permissions issue within SharePoint.

My goal was just to copy the folder as a backup so I changed


FSO.MoveFolder

To

FSO.CopyFolder

And it works great! Thank you for the inputs, sorry for the dumb question.