Android Question File.Exists(directory, filename)

luciano deri

Active Member
Licensed User
Longtime User
dim namedir, namefile as string
'namedir exists i am sure.
'namefile not valorizzed,
if File.Exists(namedir, namefile) then
'file.exists return true. Why?
'I must check namefile.lenght > 0 also ?
end if
 

luciano deri

Active Member
Licensed User
Longtime User
Most important situation is the file.delete.
The problem is the same, but in this case is deleted the directory !!
 
Upvote 0

luciano deri

Active Member
Licensed User
Longtime User
Handling files and directories is a very elementary part of B4A. If a Bug had existed it would have been found years ago.
You did not describe your problem in #1. If you want to delete a file always use:
B4X:
If File.Exists(NameDir, FileName) then File.Delete(NameDir, FileName)
Are you using your code within a Sub? "Return True" won't work outside of a sub. Please show your full code.

Try this
 

Attachments

  • TestFileInfo.zip
    7.8 KB · Views: 367
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Luciano wanted to ask another thing (I know it only because I read his post in the Italian forum :)).

If he provides an empty string as file name, the function returns True if the directory exists and Luciano thinks this is a bug.

But usually we use that way right to know if a folder exists, since there is not a function like IfDirExists or IfFolderExists.

It is worse when you use the File.Delete command: in this case, you will delete the folder (I have not tried but I trust :))
 
Upvote 0

luciano deri

Active Member
Licensed User
Longtime User
Return to begin. I reading a db that can contain a list of filename that i must delete.
I had made so
If File.Exists(NameDir, FileName) then File.Delete(NameDir, FileName)
Where filename is read from db.
filename = cursors.getstring("nomefile")
But if in filename not is valorized the App delete the folder. In the note of file.exists not specified this, so this is a bug.
Afther discovered this the correct code is
If File.Exists(NameDir, FileName) and FileName.lenght > 0 then
File.Delete(NameDir, FileName)
end if
Yours sincerely
 
Upvote 0
Top