Skip to content

Instantly share code, notes, and snippets.

@declaresub
Created August 3, 2015 15:16
Show Gist options
  • Save declaresub/4c92624e2395a8e24949 to your computer and use it in GitHub Desktop.
Save declaresub/4c92624e2395a8e24949 to your computer and use it in GitHub Desktop.
Xojo example code for backing up a folder.
Sub Backup(source as FolderItem, destination as FolderItem)
if source is nil then
dim e as new NilObjectException
e.Message = "Backup failed. source is nil."
raise e
end if
if destination is nil then
dim e as new NilObjectException
e.Message = "Backup failed. destination is nil."
raise e
end if
if destination.Exists then
dim e as new RuntimeException
e.Message = "Backup failed. destination directory already exists."
raise e
end if
destination.CreateAsFolder
if not destination.Directory then
dim e as new RuntimeException
e.Message = "Backup failed. Unable to create destination directory."
raise e
end if
dim items() as FolderItem
dim count as Integer = source.Count
for i as Integer = 1 to count
dim item as FolderItem = source.TrueItem(i)
if item <> nil then
items.Append(item)
end if
next
for each item as FolderItem in items
if item.Directory then
dim itemDest as FolderItem = destination.TrueChild(item.Name)
Backup(item, itemDest)
else
item.CopyFileTo(destination)
dim errorCode as Integer = item.LastErrorCode
if errorCode <> 0 then
dim e as new RuntimeException
e.Message = "Backup failed. Copy " + item.NativePath + " failed with error " + Str(errorCode) + "."
end if
end if
next
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment