Find first directory

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.

The documentation is your best friend:

https://documentation.xojo.com/api/files/specialfolder.html#specialfolder

Make a break point before that line and click on the nexy button
image
I’m running a prroject, not in the debugger, so the button is Grey instead of Black.

Then, look at your variable FolderItem contents and choose which instruction that fit best for your purpose.

NB: usually (by default), the application (.debug) stands in the same folder where your project resides; but this may be changed:

THis appears when you choose shared in the navigation pane:

image

This project store my .debug application there (click in the pen to set the location).

This do not change the place the build application location.

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.

At this page bottom (and nearly all other pages), there is a “See also” section where you find SpecialFolder.

Excepted if, like me, you have diffficulties to read, you will avoid to loose time to read carefully the documentation, this part included. :innocent:

Hello group, I would like to understand how to replace:

dim source as folderitem = SelectFolder

with

SpecialFolder.Resources.Parent.Parent.NativePath
(SpecialFolder.Resources.Parent.Parent.NativePath is my working folder)

in order to apply,

source.CopyFileTo(destination)

and copy my Folder.


Dim source as folderitem = SpecialFolder.Resources.Parent.Parent

But: depending on the Xojo version you are using, you bettertype Var instead of Dim.

Also, Your working folder MUST NOT BE inside the Applications folder (all OS).

in order to apply,
The given code miss information here. Do you want to copy the application somewhere ?

Explain.

At last, use a thread per subject.

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

Each time you run the project ?

Maybe better do it manually, before loading the project.

Excepted if you set explicitely the debug folder elsewhere, it is always in the same folder your project file is.

But, if you use externals, etc., they will not be saved.

In the documentation, there are matter to think / work here.

Even a Method to do what you ask (I think). You pass to it a FolderItem of the file (folder ?) to copy, and a FolderItem of the destination folder.

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.

CopyFileOrFolder(SpecialFolder.Resources.Parent.Parent.NativePath,SpecialFolder.Documents)

"Parameter “source” expects class FolderItem, but this is type String.
CopyFileOrFolder(SpecialFolder.Resources.Parent.Parent.NativePath,SpecialFolder.Documents)
"

Convert them to FolderItem and use that.

In the first place, why (or how) do you have paths ?

NO; use:
SpecialFolder.Resource.Parent.Parent

This is a FolderItem.

And, no ending ‘s’ in Resource.

My Code in elaboration…

'** 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

i modified my code…something starts to work…

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.

With an ending ‘s’: and this works ? If the answer is yes, you have to write an Issue about the documentation who wrote it without the ending ‘s’…

Yes, in MacOS, Resources means something else, historically speaking.

Yes, it’s work with s.
Without s generate error.
senzaS

Resource is used when you want something from the SpecialFolder and it needs a String, the docs say:

Var file As FolderItem = SpecialFolder.Resource("MyDb.sqlite")

if you use SpecialFolder.Resources.Parent.Parent you will end in a different Folder position if you are using Windows vs Mac

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.

1 Like