Hi all, my name is Hubert A. Klein Ikkink. Not a very common name, right? To make things easier I just picked the first letters of my firstname and surname and came up with haki. So there you have it, now I am also known as Mr. Haki or mrhaki for short. You can read more blog postings at www.mrhaki.com. I am a passionate Groovy and Java developer based in Tilburg, The Netherlands. My goal is to write clean, elegant, user-centered and high quality software. You can find me on Google+ and Twitter. Hubert is a DZone MVB and is not an employee of DZone and has posted 126 posts at DZone. You can read more from them at their website. View Full User Profile

Groovy Goodness: Calculating Directory Size

01.31.2013
| 4308 views |
  • submit to reddit

Groovy 2.1 adds the method directorySize() to File objects. If the File object is a directory then the total size of all files is calculated. Notice this method will go recursively through all subdirectories so it might take some time before the method returns. If we invoke the method on a File object that is not a directory an IllegalArgumentException is thrown.

def sampleDir = new File('sample')
def sampleDirSize = sampleDir.directorySize()

println sampleDirSize // Outputs size in bytes, eg. 130615981

Using Groovy from the command-line we can easily get the directory size like this:

$ groovy -e "println new File('.').directorySize()"

Written with Groovy 2.1


 

Published at DZone with permission of Hubert Klein Ikkink, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Fred Janon replied on Thu, 2013/01/31 - 5:43am

Cool, I didn't know. Thanks for the info.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.