Remove file first?

I did not removed the existing file, before saving, for years, but it had worked…

this is my older and worked code (copies a temporary db file):

var dbFile as FolderItem
Var defaultName as string = mScenario.Name + "-"+ format(WeekTurn,"00") + player.ToText+ format(integer(CurrentPhase), "00")
dbFile = FolderItem.ShowSaveFileDialog(FileTypes1.SPGames,defaultName)

if dbFile <> nil then
  db.DatabaseFile.CopyFileTo(dbFile)
end if

Now, I would like to make it correct so I wrote:


var dbFile as FolderItem
Var defaultName as string = mScenario.Name + "-"+ format(WeekTurn,"00") + player.ToText+ format(integer(CurrentPhase), "00")
dbFile = FolderItem.ShowSaveFileDialog(FileTypes1.SPGames,defaultName)

if dbFile <> nil then
  
  if dbFile.Exists then 
    var path as string = dbFile.NativePath
    dbFile.Remove
    dbFile = new FolderItem(path, FolderItem.PathModes.Native)
  end if
  
  db.DatabaseFile.CopyFileTo(dbFile)
end if

But things are not going as expected, sometimes when replacing an existing file, the result is no file at all.
What is incorrect?

What does this produce?

if dbFile <> nil then

if dbFile.Exists then
dbFile.Remove
if dbfile.exists then
msgbox “Didnt get deleted”
else

db.DatabaseFile.CopyFileTo(dbFile)
end if
end if

It removes the file as expected but not creates the new one.

I’m suspicious the problem maybe comes from
db.DatabaseFile.CopyFileTo(dbFile)

Sorry, was my fault, the problems comes from
db.DatabaseFile.CopyFileTo(dbFile)

But, what is more correct code, the original, or the second one, which first removes the file?

Does it exist?
Is it nil?
Is it the same folderitem?

Well the problem was that in the original code the file was never removed and so it worked.

When I tried to improve the code, removing first when
db.DatabaseFile.CopyFileTo(dbFile)
In some situations the file to be copied was the file that was just removed, so nothing was CopyTo, giving as result a none file.

I come back to

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

Which Xojo version do you use ?

2021r2

the problems comes from:
db.DatabaseFile.CopyFileTo(dbFile

And was my wrong code.

I’d be suspicious of this. Possibly the dbFile you are creating in the If block goes out of scope with the end of the block. I’d set a breakpoint at the CopyFileTo statement and verify that dbFile is still valid.

If I look at 2021r1.1 Documentation for FolderItem.CopyFileTo:

If Destination is a file and the file already exists, the copy is cancelled. You need to delete the existing file first. If there is an error, the LastErrorCode property contains an error code.

How come that would have worked in the past ?

But the Documentation also says:

This item was deprecated in version 2019r2.1 .
Please use FolderItem.CopyTo as a replacement.

It’s still the same, the file needs to be first deleted if it exists:

If destination is a file and the file already exists, the copy is cancelled. You need to delete the existing file first. If there is an error, an IOException is raised.

Both ways, the copy should not work, but with CopyTo, that will not be unnoticed.

The first one works.

Thing is, the file that was ‘being copied’ was the same file.
Copy or no, the file existed.
Perhaps what was actually achieved was a filename change.

The problem reported above was that after delting the target file, there was no end product
Because the target file and the source file were the same file, what was deleted was both target and source.
Nothing to copy.

Yes, exactly this.
In some very special cases the source and target could be the same file and, after deleting it there was nothing to copy.
In my first code, as never gets deleted the file, if source and target were the same nothing happened.