Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get number of files in a directory and its subdirectories

Tags:

java

android

using this code

new File("/mnt/sdcard/folder").listFiles().length

returns a sum of folders and files in a particular directory without caring about subdirectories. I want to get number of all files in a directory and its subdirectories.

P.S. : hardly matters if it returns a sum of all the files and folders.

any help appreciated, thanks

like image 742
jeet.chanchawat Avatar asked Sep 27 '12 07:09

jeet.chanchawat


1 Answers

Try this.

int count = 0;
getFile("/mnt/sdcard/folder/");

private void getFile(String dirPath) {
    File f = new File(dirPath);
    File[] files = f.listFiles();

    if (files != null)
    for (int i = 0; i < files.length; i++) {
        count++;
        File file = files[i];

        if (file.isDirectory()) {   
             getFile(file.getAbsolutePath()); 
        }
    }
}

It may help you.

like image 200
Hemant Avatar answered Oct 28 '22 11:10

Hemant