Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate Directory Size - is there a smart way?

Status
Not open for further replies.

icecuber

Programmer
May 13, 2005
25
0
0
IT
I have to Calculate Directory Size, the trivial method is to open all subdirectory recursively and add the size of all the file the dir contains... this may be VERY LONG and DISK-INTENSIVE...

there's another way? or the trivial solution is the best??

thanks in advice?
 
Inside the Commons IO - package org.apache.commons.io

i find the Class FileUtils
that has a method that implements the trivial solution :
sizeOfDirectory

public static long sizeOfDirectory(File directory)

Recursively count size of a directory (sum of the length of all files).

Parameters:
directory - directory to inspect
Returns:
size of directory in bytes, 0 if directory is security restricted


for now i'm going to use this
 
If you don't need it to be cross-platform you can perform an OS operation with System.exec().

Anyway, I think even the OS way to calculate that is recursively do it for every subdir.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top