File.delete not working

I am using the CopyFileOrFolder function from the Language Reference. Having a problem getting an existing file, I want to use as the destination file, deleted.

I have added Msgbox items to help me determine what’s going on. All folder items are not Nil and are correctly pointing to the correct folder/file combinations. However, I get the message “still here” and an error code of -1407.

Appreciate any pointers, suggestions, etc….

[code] // it’s not a folder
// it’s a file
If ( destination.Exists ) Then
destination.Delete
End

If ( destination.exists ) Then
  msgbox "still here"
End If

source.CopyFileTo destination
MsgBox "Error code = " + str ( source.LastErrorCode )
If ( source.LastErrorCode <> 0 ) Then
  Return False
End If

End If

Return True[/code]

TIA, Tony

What kind of file is it, and do you have permissions to delete?
You say ‘its not a folder’ but your code above doesn’t show any check for that condition.
Might it be a bundle? (a special folder)
Might it be in use by some other application holding it open?

File locked by another application.
Insufficient file permissions.
It is in the end a folder you’re trying to delete, not a file.

File is NOT locked, file permissions are OK. It is a file I’m trying to delete (which is why I only showed the code for it’s a file).

Jeff, I’m using the complete function as described in the Language Ref and it does test for folder (above the code I included). Not a special folder or bundle - just a simple file in a folder combination. That’s why it’s driving me nuts.

No other application has control. I checked all the possibilities you’ve mentioned before I posted.

  • Tony

Tony,

I recall having trouble with f.delete in the past (RB days, OS X) so I always use this extends function f.strongDelete that I wrote which never fails:

’ sometimes RB’s f.delete fails! Use this UNIX cmd to force a file delete. Will delete non-empty folders too
’ IMPORTANT: if f is a folder NEVER REUSE f AFTER THIS CALL because f will now point to the parent directory of f, so any other delete will operate on the enclosing directory of f with disastrous results. Must re-create f to re-use

dim sh as new Shell
dim cmd as string
sh.mode = 0 ’ synchronous
cmd = "rm -fdr " + f.ShellPath ’ -r means folders too, -d mean unlink (?)
sh.Execute cmd
return

(I dont know what the -d flag does exactly but sounded like a good idea…)

Note the (VERY) important caveat about reusing f after the call if f is a folder (which as you can imagine I learned the hard way). You can always trap f.Directory and return if you wish. Not sure what your issue is but this should work for you under all circumstances (OS X anyway).

P.

The error is the following:

errFSNotAFolder               = -1407, /* Expected a folder, got a file */

But this is related to

source.CopyFileTo destination

and not the deleting, since you test for the error after the CopyFileTo statement.

Thanks Eli. Where did you get the error code explanation? I Googled but did not find.

  • Tony

Thanks Peter.

However, the function tests for folders at the top and skips to the else (that I showed above) that says it is a FILE. The function as written in the Language Ref, and as I traced in debug, properly sees destination as the correct file in the correct folder. So why the -1407 expected a folder?

So where is the issue?

  • Tony

Hello guys, im trying to delete a file from a location on the stick and i get an 101 error, as far as i checked its seems that im not getting what this error means. Any idea if the folderItem.delete part was fixed, it seems that im getting an error. Im currently using XOJO 2014R3.

Thnaks