SaveFileDialog does not delete the previous file

The code shared in SaveFileDialog does not delete the file if it is alreasy existing.

How do I know that ?

When I add a TABLE to the file, I get an error: the TABLE already exists (when I save the same data into the file).

I know I can add Exists in CREATE TABLE IF NOT EXISTS (or so), but there is an error there.


[Var](xojo://Var) dlg [As](xojo://As) [New](xojo://New) SaveFileDialog
[Var](xojo://Var) saveFile [As](xojo://As) FolderItem
dlg.InitialFolder = SpecialFolder.Documents
dlg.PromptText = "Prompt Text"
dlg.SuggestedFileName = "Suggested Filename"
dlg.Title = "Title Property"
dlg.Filter = FileTypeGroup1.SQLite
saveFile = dlg.ShowModal
[If](xojo://If...Then...Else) saveFile <> [Nil](xojo://Nil) [Then](xojo://If...Then...Else)
// saveFile is the FolderItem of the file to save
// user canceled
[End If](xojo://If...Then...Else)

Xojo 2021r1.1
High Sierra

What code?
Are you talking about shared methods in a class?
What Class?

The SaveFileDialog ONLY gives you info about the selected item, not performing actions.

So you just trow data in to a file you dont even know if the file is new or not? :scream:

What is this “code” suposed to do?

1 Like

I think is a copy/paste from:

How long have you been using Xojo that you don’t know this?


the Dialog give only a FolderItem or not.
usually you overwrite the file then.

@Ivan_Tellez: this is helpless.

That is the code that appears in SaveFileDialog.

Only change is the File Type Group (to save as a .sqlite file).

What is this “code” suposed to do?
Read the docs. And if you do not like how it comes, copy the code from the LR and paste it here to understand, as Alberto said.

No application do that. When you save a document in an already existing file, you are warned and asked if you want to overwrite the existing file. If yes, the old file is destroyed and replaced by a new; this is not the case.

There is no code to use to delete the file.

Try by your self with code to save a data base and try the sam with code to save text file:

Pseudo code to save as text file (*):
saveTOS = TextOutputStream.Save(saveFile)
saveTOS.Write “Text data”

In this condition the previous contents is replaced.

How long are you using Xojo (and a computer) to not understand ?

(*) Real code exists in LR TextOutputStream entry.

Are you OK? It is strange that you do not understand something this basic :expressionless: Tim already answered you.

This is like complaining that the IDE didnt created tables and populated with data you have in your mind, just by declaring a database variable.

The dialog only returns the reference to a FolderItem, you are suposed to check if the file EXISTS and delete it

Have you searched for DELETE in the documentation?
(Another wortless api 2 name change with no real beneffit but still apears as deprecated.)

the behavior of the dialog is
if you select a new file the dialog return the folderitem to use later
if you select a existing file the dialog show a warning, ok,ok return the folderitem else select other or cancel.
cancel return nil

if you overwrite the file or append your file then is your part.
its not the job of a file dialog to delete things.

That’s because all the other applications check for the existance of the file and delete it if it’s already there, or simply overwrite it. You’re a programmer. It’s up to you to delete the file if that’s what you desire. No dialog will actually delete the file in any application. It’s all done afterward before the new contents are written out.

1 Like

The night was good and prolific. The day was long and in the evening I wrote:

// Global Property
// gSQLite_FI As FolderItem
Var Save_Dlg As New SaveFileDialog

// Set the SaveFileDialog properties
Save_Dlg.InitialFolder     = SpecialFolder.Documents
Save_Dlg.PromptText        = "Choose"
Save_Dlg.SuggestedFileName = "My_SQLite DB.sqlite"
Save_Dlg.Title             = "Create a SQLite File"
Save_Dlg.Filter            = FTG_SQL.sqlite

mSQLite.gSQLite_FI = Save_Dlg.ShowModal
If mSQLite.gSQLite_FI = Nil Then
  // User canceled
End If

// Delete the file if it is not empty
If mSQLite.gSQLite_FI.Length > 0 Then
End If

Looks strange, never believe this have to be done n my 42 years in the computing industry, but this works for SQLite database saving.

Why the OS ask me if I want to replace the previous file is a mysery (Err, typo: mystery).

You sure it’s the OS doing it (for other applications) rather than the app itself?

The OS asks if you want to replace the file as a courtesy in the dialog. If you say OK, then the dialog proceeds and returns the FolderItem. If you say Cancel, then the dialog continues to prompt for a file name and location. That is the function of the SaveFileDialog. The dialog itself is not going to delete the file. That would be potentially bad - suppose your app just wanted to append to it. It’s up to your app to decide what to do with the folderitem - delete, append, merge information, whatever. It has always been this way. I think if you review some of your old code, you will find that you were testing for Exists and deleting if needed.

You are still going to find all kind of problems comparing the Length of the file instead of if just exists or not and not having a check to see if you can actually delete the file. (It could be in use by another app)