I am using the following code to calculate the number of files in a folder that has many sub-folders and files within. I also calculate the total size of all files in MB.
/*these two are global variables*/
int filesCount = 0;
double folderSize = 0;
/*all this within main method*/
getFilesDetails("D:\\Dataset\\Linux_Dataset");
System.out.println("\nTotal number of files processed: "+filesCount);
System.out.println("Total size of processed files: "+folderSize+" MB\n\n");
public void getFilesDetails(String location) throws NoSuchAlgorithmException, IOException {
File directory = new File(location);
File[] files = directory.listFiles();
if (files!=null) {
for (File f : files) {
if (f.isFile()) {
filesCount++;
folderSize += f.length()/1048567.0;
} else if (f.isDirectory()) {
getFilesDetails(f.getAbsolutePath());
}
}
}
}
In case of having small number of files the result is accurate. I had a folder that had total of 76176 files and total 833 MB size, and the result got was also same as below,
Total number of files processed: 76176
Total size of processed files: 833.3733943562565 MB
But when i add more files and folders the result is not accurate. I had total of 10,70,493 files in properties with total size 10.9 GB. But when i run the code it shows 10,80,477 and the total size obtained is also wrong.
Total number of files processed: 1080477
Total size of processed files: 11248.867834882847 MB
Is there any other accurate way to calculate this even if the number of files is in millions and with size more than 10 GB? Thank you.
This is my input folder properties... Original input folder priperties