Get a list of all the files in a directory (recursive)
The following works for me in Gradle / Groovy for build.gradle
for an Android project, without having to import groovy.io.FileType
(NOTE: Does not recurse subdirectories, but when I found this solution I no longer cared about recursion, so you may not either):
FileCollection proGuardFileCollection = files { file('./proguard').listFiles() }
proGuardFileCollection.each {
println "Proguard file located and processed: " + it
}
Newer versions of Groovy (1.7.2+) offer a JDK extension to more easily traverse over files in a directory, for example:
import static groovy.io.FileType.FILES
def dir = new File(".");
def files = [];
dir.traverse(type: FILES, maxDepth: 0) { files.add(it) };
See also [1] for more examples.
[1] http://mrhaki.blogspot.nl/2010/04/groovy-goodness-traversing-directory.html
This code works for me:
import groovy.io.FileType
def list = []
def dir = new File("path_to_parent_dir")
dir.eachFileRecurse (FileType.FILES) { file ->
list << file
}
Afterwards the list variable contains all files (java.io.File) of the given directory and its subdirectories:
list.each {
println it.path
}