While looking for a script that display directory tree, I came across this Groovy script.
The code look just fine but it don't work (I test it with Groovy 1.7). After some google, I found that this is due to the closure as a first class object in Groovy, it can't be call within the closure itself.
And below is the solution:
def listRecursivly
listRecursivly = { file, arg ->
println "${'| ' * arg}`- ${file.getName()}\\"
file.eachFile() {f ->
if(f.directory) {
listRecursivly(f,(arg + 1))
} else {
println "${'| ' * (arg + 1)}`- ${f.getName()}"
}
}
}
listRecursivly (new File("."),0)
The little trick here is to define and initialize the closure in 2 separate lines.

No comments:
Post a Comment