Hello group, I need to understand which root directory contains the program … now it is located at c:\user\test\desktop\MyProgram\ here is the xojo program that I am building with the various folders .
I need to copy these folders to another place. I managed to select the destination folder, but I don’t understand how to find the source one.
With:
Dim f As FolderItem
f = GetFolderItem(“”)
msgbox(f.NativePath) is the directory c:\user\test\desktop\MyProgram\debug
I don’t want to select the folder (which I managed to do), but I would like the program to find it automatically.
WOW, I had read the FolderItem documentation, but not this page…there are so many options I hadn’t considered. I will read and try to understand the guide.
Thanks for now.
Yes, I would like to copy the entire working folder where the project resides (the path I managed to recover with SpecialFolder.Resources.Parent.Parent.NativePath), and copy it to a backup folder that I have on my desktop. I’m not a programmer but I developed little things for me with the old VB6, where I put the path of type string. With Xojo I’m a bit disoriented…
I have to copy the folder as it is, in a folder that from time to time will have the name of Backp_dd_mm_yyyy
Yes, I have a source path and a destination path … I would like that when I press a button it will backup me in the destination folder with name Backup_dd_mm_YYYY.
I tried to use the procedure for copying files and folders, but it requires folderitem , instead I have string. I understand that the problem is with my code, but I don’t understand how to derive the right path in the right format.
"Parameter “source” expects class FolderItem, but this is type String.
CopyFileOrFolder(SpecialFolder.Resources.Parent.Parent.NativePath,SpecialFolder.Documents)
"
'** My Program PATH
Dim f As New FolderItem '("", FolderItem.PathModes.Native)
f = f.Parent
messagebox("Sorgente " + f.parent.NativePath)
'******* choose destination ********************************
Var dlg As New SelectFolderDialog
dlg.ActionButtonCaption = "Select cartella di destinazione"
dlg.Title = "Backup di sicurezza"
dlg.PromptText = "Backupo di sicurezza"
dlg.InitialFolder = SpecialFolder.Documents
Var f1 As FolderItem
f1 = dlg.ShowModal
If f1 <> Nil Then
// It's OK , copy files and folders
if CopyFileOrFolder(f.Parent,f1.Parent)=True then
Messagebox " Attenzione, backup ok ..."
Else
// Non serve più copiare nulla per decisione dell'utente
End If
end if
CopyFileOrFolder is sample code copied by xojo documentation
Var origin, destination As FolderItem
origin = Folderitem.ShowSelectFolderDialog
If origin <> Nil Then
destination = Folderitem.ShowSelectFolderDialog
If destination <> Nil Then
CopyFileOrFolder(origin, destination) '// See below
MessageBox("Copy complete!")
End If
End If
Sub CopyFileOrFolder(source As FolderItem, destination As FolderItem)
Var newFolder As FolderItem
If source.IsFolder Then // it's a folder
newFolder = destination.Child(source.Name)
newFolder.CreateFolder
For Each file As Folderitem In source.Children // go through each item
If file.isFolder Then
// it's a folder
CopyFileOrFolder(file, newFolder) // recursively call this routine passing it the folder
Else
file.CopyFileTo(newFolder) // it's a file so copy it
End If
Next
Else // it's not a folder
source.CopyFileTo(destination)
End If
End Sub
Var origin, destination As FolderItem
origin = specialfolder.Resources.Parent.Parent
If origin <> Nil Then
Var dlg As New SelectFolderDialog
dlg.ActionButtonCaption = "Select cartella di destinazione"
dlg.Title = "Backup di sicurezza"
dlg.PromptText = "Backup di sicurezza"
dlg.InitialFolder = SpecialFolder.Documents
destination = dlg.ShowModal 'Folderitem.ShowSelectFolderDialog
If destination <> Nil Then
Dim d as date
Dim NewFolderName as string
' Backup alone, it's work --------- Backup+d.month.tostring go to error. WHY ?
NewFolderName="Backup" + d.Month.ToString
destination=destination.Child(NewFolderName)
destination.CreateFolder
CopyFileOrFolder(origin, destination) '// See below
So it seems to work, it copies the whole folder, subfolders and files where the program resides. Now, I would like to set the destination directory with a particular name consisting of dd_mm_yyyy. A normal name works, composed like this, no, it gives me an error. Why ?
Var origin, destination As FolderItem
Dim SourcePath as string
Dim DestinationPath as string
Var d As DateTime = DateTime.Now
Dim NewFolderName as string
NewFolderName="Backup_"+d.Day.ToString+"_"+d.Month.ToString+"_"+d.Year.ToString
origin = specialfolder.Resources.Parent.Parent
SourcePath=origin.Nativepath ' Converto il FolderItem in STRING
If SourcePath <> "" Then
Var dlg As New SelectFolderDialog
dlg.ActionButtonCaption = "Seleziona e crea la cartella "+NewFolderName
dlg.Title = "Backup di sicurezza - Selezionare la cartella contente tutti i BACKUP -"
dlg.PromptText = "Backup di sicurezza"
dlg.InitialFolder = SpecialFolder.Documents
destination = dlg.ShowModal 'Folderitem.ShowSelectFolderDialog
If destination <> Nil Then
destination=destination.Child(NewFolderName)
DestinationPath=destination.NativePath
if destination.Exists then
messagebox " La cartella già esiste. Eliminarla manualmente o rinominarla"
return
end if
if MsgBox("Copiare la cartella " + SourcePath +" su " + DestinationPath + "?" , 36)=6 then
if CopyFileOrFolder(SourcePath, DestinationPath)=true then '// See below
MessageBox("Copia completata!")
else
MessageBox("ERRORI durante la copia!")
end if
End If
End If
end if
I was able to copy the entire folder to a folder of my choice named after the day the backup is done.
I bypassed the folder existence issue by warning the user.
Is there an easy way to overwrite the whole folder in case it already exists?
As a recomendation, I would use Backup_YYYY_mm_dd. This sorts naturally in the directory listing and will allow you to identify the most recent backup(s) quickly.
I seem to recall that if you try to USE a SQLlite file that is in resources, it will be read-only and won’t work properly.
It should be copied to specialfolder.applicationdata.child(“Yourapp”) and used from there.