public static long countAllFilesInDirectory ( final File dir , long length , final boolean includeDirectories ) { // Get all files final File [ ] children = dir . getAbsoluteFile ( ) . listFiles ( ) ; if ( children == null || children . length < 1 ) { return length ; } for ( final File element : children ) { // if the entry is a directory if ( element . isDirectory ( ) ) { // then // if directories shell be include if ( includeDirectories ) { // then // increment the length length ++ ; } // find recursively in the directory the files. length = countAllFilesInDirectory ( element , length , includeDirectories ) ; } else { // increment length... length ++ ; } } return length ; }
Counts all the files in a directory recursively . This includes files in the subdirectories .