CopyTo

I’m lost, what is wrong there?

dbFile = FolderItem.ShowSaveFileDialog(FileTypeGroup1.spGame, defaultName)

if dbFile <> nil then
  if dbFile.Exists then dbFile.Remove
  db.DatabaseFile.CopyTo(dbFile)
end if

The result is that the existing file is deleted but the new one is missing.

I wonder if the path of dbFile is being changed to the Trash once Remove is called?

If db.DatabaseFile is connected then it is possibly locked so you may have to close it first.

If its a SQLite file then there is a backup API available.

1 Like

Have you checked whether this file even exists (as reported in the debugger)?

Yes the file exists.
Is there an example on how to save a file (with ShowSaveFileDialog) then click on an existing file to get its name, and then save?

I don’t have experience with FolderItem.Remove and after reading your comment I was doing a test.

The file got removed and is not in the Trash. It doesn’t look like the path is being changed.


dbFile = FolderItem.ShowSaveFileDialog(FileTypeGroup1.spGame, defaultName)

if dbFile <> nil then
  if dbFile.Exists then 
    var Dummy as new FolderItem(dbFile.NativePath,FolderItem.PathModes.Native)
    dbFile.Remove
    db.DatabaseFile.CopyTo(Dummy)
  else
    db.DatabaseFile.CopyTo(dbFile)
  end if
end if

This also not works. If the file exists it is removed a no new file saved.
Is the file do not exist (a new name) then the two files are ok, the original and the new with a new name.
What is wrong there?

file.Remove removes the file not move it to the trash (MacOS)

1 Like

Thanks.

Kem’s comment made me believe that it may change the path because it was moved to the Trash. Now I know it doesn’t move to the trash.

Sorry for the noise. If my tests gives an idea of what is happening I will post here.

1 Like

I don’t know if this helps but I used the SQLite sample that comes with Xojo and added a button with this code:

Code
Var dialog As SaveFileDialog
Var file As FolderItem

// Create the dialog (does not actually show it)
dialog = New SaveFileDialog

// The filter is what type of file you are saving (for cosmetic purposes)
// The filters are defined in the File Types dialog
// inside the edit menu
dialog.Filter = "text/plain"

// The OpenDialog class supports custom prompts…
dialog.PromptText = "Here's where the prompt goes"

// …and custom title's
dialog.Title = "This is the Title"

// Set the suggested file name
dialog.suggestedFileName="MyFile 1"

// Custom cancel captions are available also
dialog.CancelButtonCaption = "NO!!!!"

// And custom ok button captions
dialog.ActionButtonCaption = "Save It!"

// Also, you can display the dialogs as sheets
// by calling dialog.ShowModalWithin(Self)
// 
// For regular modal dialogs, just call dlg.showModal
file = dialog.ShowModal

//then test for nil to see if the user clicked cancel
If file <> Nil Then
  If file.Exists Then file.Remove
  app.db.DatabaseFile.CopyTo(file)
End If

image

I got asked a couple of times if I let the app read/write to a folder and it worked correctly.

Then I changed the code to:

Code
'Var dialog As SaveFileDialog
Var file As FolderItem

// Create the dialog (does not actually show it)
'dialog = New SaveFileDialog

// The filter is what type of file you are saving (for cosmetic purposes)
// The filters are defined in the File Types dialog
// inside the edit menu
'dialog.Filter = "text/plain"

// The OpenDialog class supports custom prompts…
'dialog.PromptText = "Here's where the prompt goes"

// …and custom title's
'dialog.Title = "This is the Title"

// Set the suggested file name
'dialog.suggestedFileName="MyFile 1"

// Custom cancel captions are available also
'dialog.CancelButtonCaption = "NO!!!!"

// And custom ok button captions
'dialog.ActionButtonCaption = "Save It!"

// Also, you can display the dialogs as sheets
// by calling dialog.ShowModalWithin(Self)
// 
// For regular modal dialogs, just call dlg.showModal
'file = dialog.ShowModal

file = FolderItem.ShowSaveFileDialog("text/plain", "default")

//then test for nil to see if the user clicked cancel
If file <> Nil Then
  If file.Exists Then file.Remove
  app.db.DatabaseFile.CopyTo(file)
End If

image

but this time it didn’t remove the file and saved the new file without an extension.


Edit: Wow, there is a difference if I single click to select the file then a new file is created without extension, if I double click the file I want to overwrite, then I get a dialog saying that it will overwrite the file. There is no visual difference on the dialog itself if I single click or double click. Is this a known feature/bug?

Using macOS 13.6.8, Xojo 2024r2.1

The exemple works but it is not exactly my case.

SaveFileDialogTestLoadSave.xojo_binary_project.zip (17.5 KB)

A modified example,

  • Create and save a DB
  • Quit
  • Open and load recently saved DB
  • Save the loaded DB with same name as loaded
    The file disapears

In that part of code, you don’t have to create a dummy folderitem. Just reuse the removed dbFile:

dbFile.Remove
db.DatabaseFile.CopyTo(dbFile)

That makes your code more readable and easy to follow.

Anyway, I tried your whole code. As I don’t have the same files/classes that you have, I modified it slightly, but it won’t matter. Here’s the code on my side, using the way you did it (for reference):

Var dbFile As FolderItem = FolderItem.ShowSaveFileDialog(FileTypeGroup1.Any, "test.plist")
Var OtherFile As new FolderItem("/Users/anic/another.plist")

if dbFile <> nil then
  if dbFile.Exists then 
    var Dummy as new FolderItem(dbFile.NativePath,FolderItem.PathModes.Native)
    dbFile.Remove
    OtherFile.CopyTo(Dummy)
  else
    OtherFile.CopyTo(dbFile)
  end if
end if

Everything worked fine. The existing “test.plist” was first removed and the file I selected replaced it. So, except the unnecessary “dummy” variable, there’s nothing wrong with your code.
That leads to a specific issue on your side. Are your files stored on an external disk, or remote network share? Possibly a different file system? :man_shrugging:
In other words, what’s different than the working way?

This was my original code, but not worked

dbFile.Remove
db.DatabaseFile.CopyTo(dbFile)

Have you tried the
SaveFileDialogTestLoadSave.xojo_binary_project.zip (17.5 KB)

The only sad solution I found is

dbFile = dialog.ShowModal

if dbFile <> nil and dbFile.Exists then
  MessageBox "Use another name"
  return
  // dbFile.Remove
else
  db.DatabaseFile.CopyTo(dbFile)
end if

Checking old apps I found that this code worked and still works

if dbFile <> nil then
  // if dbFile.Exists then dbFile.Remove This is not needed
  db.DatabaseFile.CopyFileTo(dbFile)
end if

No need to remove the existing file before using the new file.CopyTo

db.DatabaseFile.CopyTo(dbFile)

So, I have no solution for updating old apps
Is there anyone from Xojo stuff herre?, it’s a bug?, Am I doing wrong?

Your modified code doesn’t work either, so I’m not convinced yet it should make any difference.

I know nothing about databases, so I don’t know how to try this project, sorry.
And you, have you tried my code?

Certainly the sad solution you found can be avoided. In the worst case where we can’t find the reason of the failure, you’ll always be able to move the existing file to the trash (or temporary folder) and then write the new file (except on Windows where an open file can’t even be renamed).
I’m suspecting that the database is keeping the file open, but I can’t be sure, since I’m not even interested in learning databases.

Yes, not working either

With dummy plist files in your home folder it doesn’t work?
Then either your operating system is corrupted or you’re doing something in a very unusual way :thinking:.

I created an exemple, may someone try it and tell if get the same results as I?

The procedure is the main window.
SQLiteLoadSave.xojo_binary_project.zip (18.3 KB)