0

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

Mano Prathibhan C
  • 488
  • 1
  • 11
  • 38
  • 1
    Possible duplicate of [Counting the number of files in a directory using Java](http://stackoverflow.com/questions/687444/counting-the-number-of-files-in-a-directory-using-java) – acm Sep 06 '16 at 05:56
  • are you sure that there are no hidden folders or files in your folders?? – Abhishek Sep 06 '16 at 06:03
  • 2
    Your 10.9 GB versus 11248.867834882847 MB is correct, when you remember that there is (sometimes, depending on who is counting!) 1024 MB in a GB, so 11248/1024 = 10.984. – Ken Y-N Sep 06 '16 at 06:04
  • @Abhishek yes im sure there is no hidden files.. – Mano Prathibhan C Sep 06 '16 at 06:07
  • @KenY-N oh ok..there is still the files count issue – Mano Prathibhan C Sep 06 '16 at 06:08
  • What did you do to count the number of files outside of Java? How did you come up with the number 1,070,493? – clstrfsck Sep 06 '16 at 06:11
  • Also, you have the last two digits in your megabytes divisor transposed. 1 MiB = 2^20 bytes = 1,048,576 bytes, not 1,048,567. – clstrfsck Sep 06 '16 at 06:14
  • @msandiford i saw that in system properties..right click the root folder and check properties – Mano Prathibhan C Sep 06 '16 at 06:18
  • Where are you getting the "correct" count from? Does that include directories too? – Andy Turner Sep 06 '16 at 06:18
  • @AndyTurner i have added the system properties image in question.. 10,70,493 files and 68,832 folders – Mano Prathibhan C Sep 06 '16 at 06:24
  • 1
    @Mano can you run `find yourdirectory -type f | wc -l` (or windows equivalent), just to make sure that the properties window isn't lying... – Andy Turner Sep 06 '16 at 06:27
  • I also wonder if, since there is `Linux_Dataset` mentioned, are there symbolic links? You can do symbolic links on NTFS, and there might be something useful [in this question](https://stackoverflow.com/questions/813710/java-1-6-determine-symbolic-links). – Ken Y-N Sep 06 '16 at 06:28

0 Answers0