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.
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?
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?
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
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
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?
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?
In other words, what’s different than the working way?
dbFile = dialog.ShowModal
if dbFile <> nil and dbFile.Exists then
MessageBox "Use another name"
return
// dbFile.Remove
else
db.DatabaseFile.CopyTo(dbFile)
end if
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.
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 .