Beginner
Beginner

Reputation: 29573

How to check if a folder is empty

I currently need to send files from a folder, i want the service i am running to check in the folder every half an hour....how am i able to know if the folder is clear?

Upvotes: 12

Views: 22070

Answers (3)

john personna
john personna

Reputation: 442

if you have the path, you can make a File object check for entries (using file.isDirectory() and file.list())

Upvotes: 2

Will Tate
Will Tate

Reputation: 33509

if (file.isDirectory()) {
    String[] files = file.list();
    if (files.length == 0) {
        //directory is empty
    }
}

Upvotes: 7

I82Much
I82Much

Reputation: 27326

File directory = new File("/path/to/folder");
File[] contents = directory.listFiles();
// the directory file is not really a directory..
if (contents == null) {

}
// Folder is empty
else if (contents.length == 0) {

}
// Folder contains files
else {

}

Upvotes: 35

Related Questions