Friday, August 25, 2006

Groovy: Get all XML files from a directory tree

Currently I'm working in a project, where Groovy (a dynamic scripting language for the JVM) is used. One of the project members is Dierk König, which is a Groovy commiter and the author of Groovy in Action. This gives a real insight in the beauty of Groovy :)

Below you can see a nice solution for finding all XML files in a directory tree:


// pass a directory or use the current directory
def folder = args?.size() ? args[0] : "."
println "reading files from directory '$folder'"
def basedir = new File(folder)

// result files of the current run
files = basedir.listFiles().grep(~/.*xml$/)

List resultLines() {
List result = []
for (currentFile in files) {
println " processing $currentFile"
}
return result
}

2 comments:

Anonymous said...

Good one.

Phoenix said...

Very useful! Thanks a ton!