Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a folder is empty

Tags:

android

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?

like image 891
Beginner Avatar asked Jan 20 '11 14:01

Beginner


People also ask

How do you check the folder is empty or not?

To check whether a directory is empty or not os. listdir() method is used. os. listdir() method of os module is used to get the list of all the files and directories in the specified directory.

How do you check if a folder is empty or not in shell script?

There are many ways to find out if a directory is empty or not under Linux and Unix bash shell. You can use the find command to list only files. In this example, find command will only print file name from /tmp. If there is no output, directory is empty.

How do you check a folder is empty or not in Java?

File. list() is used to obtain the list of the files and directories in the specified directory defined by its path name. This list of files is stored in a string array. If the length of this string array is greater than 0, then the specified directory is not empty.


3 Answers

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 {

}
like image 129
I82Much Avatar answered Oct 04 '22 21:10

I82Much


if (file.isDirectory()) {
    String[] files = file.list();
    if (files.length == 0) {
        //directory is empty
    }
}
like image 33
Will Tate Avatar answered Oct 04 '22 21:10

Will Tate


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

like image 32
john personna Avatar answered Oct 04 '22 21:10

john personna