April 28, 2010

Groovy Goodness: Traversing a Directory

Groovy adds the traverse() method to the File class in version 1.7.2. We can use this method to traverse a directory tree and invoke a closure to process the files and directories. If we look at the documentation we see we can also pass a map with a lot of possible options to influence the processing.

import static groovy.io.FileType.*
import static groovy.io.FileVisitResult.*

def groovySrcDir = new File(System.env['GROOVY_HOME'], 'src/')

def countFilesAndDirs = 0
groovySrcDir.traverse {
println "Total files and directories in ${groovySrcDir.name}: $countFilesAndDirs"

def totalFileSize = 0
def groovyFileCount = 0
def sumFileSize = { 
    totalFileSize += it.size()
def filterGroovyFiles = ~/.*\.groovy$/
groovySrcDir.traverse type: FILES, visit: sumFileSize, nameFilter: filterGroovyFiles
println "Total file size for $groovyFileCount Groovy source files is: $totalFileSize"

def countSmallFiles = 0
def postDirVisitor = {
    if (countSmallFiles > 0) {
     println "Found $countSmallFiles files with small filenames in ${it.name}"
    countSmallFiles = 0
groovySrcDir.traverse(type: FILES, postDir: postDirVisitor, nameFilter: ~/.*\.groovy$/) {
    if (it.name.size() < 15) {