B4J Question B4J File.exists question

thierry6031

Member
Licensed User
Longtime User
Hello

I have a question about the file.exists method.

Is it possible to use that method to check for an empty folder?
For example :

B4X:
if file.exists (FolderName, "*.txt") then
' code if exists
else
' code if no txt file found
endif

I tried it that way, but I always get false (meaning no file found) even if there are actually files.

I want to prevent the user to overwrite existing file or to try to handle empty folders.

Thanks
Thierry
 

thierry6031

Member
Licensed User
Longtime User
That's what I thought.

But Has anybody a solution for this?

I was thinking about using the file.listfiles method, and then checking for the list.size property to verify.
But this supposes the folder to contain only the expected files or exactly nothing.

Thanks
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
But Has anybody a solution for this?
the sinplest solution to check if a folder is empty is to get the list of files from this folder and check if it is "empty".
B4X:
dim flist as List = File.ListFiles(folder)
Note that it also contains Files like "." or ".."...

You probably also can use (or at least get some inspiration from) the WildcardFileListing class.
 
Last edited:
Upvote 0
Top