108

I'm trying to get (not print, that's easy) the list of files in a directory and its sub directories.

I've tried:

def folder = "C:\\DevEnv\\Projects\\Generic";
def baseDir = new File(folder);
files = baseDir.listFiles();

I only get the directories. I've also tried:

def files = [];

def processFileClosure = {
        println "working on ${it.canonicalPath}: "
        files.add (it.canonicalPath);
    }

baseDir.eachFileRecurse(FileType.FILES, processFileClosure);

But "files" is not recognized in the scope of the closure.

How do I get the list?

lospejos
  • 1,976
  • 3
  • 19
  • 35
Yossale
  • 14,165
  • 22
  • 82
  • 109

5 Answers5

244

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
}
Christoph Metzendorf
  • 7,968
  • 2
  • 31
  • 28
29

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

Oscar Scholten
  • 570
  • 5
  • 13
6

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
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ChrisPrime
  • 458
  • 1
  • 6
  • 13
  • 1
    although this probably does not recurse through subdirectories. However: worked for my purposes for separating out proguard files and importing them all at once :) – ChrisPrime Apr 28 '16 at 18:12
  • Unfortunately this does not answer the question "all the files in a directory (recursive)". It will only list the current directory and is misleading in the context. – ottago Jun 04 '16 at 06:28
  • `fileTree` recurses. – Noel Yap Aug 24 '17 at 16:56
  • `FileTree` does not include directories (not treating them as files). – DNax Dec 21 '17 at 15:39
3

This is what I came up with for a gradle build script:

task doLast {
    ext.FindFile = { list, curPath ->
        def files = file(curPath).listFiles().sort()

        files.each {  File file ->

            if (file.isFile()) {
                list << file
            }
            else {
                list << file  // If you want the directories in the list

                list = FindFile( list, file.path) 
            }
        }
        return list
    }

    def list = []
    def theFile = FindFile(list, "${project.projectDir}")

    list.each {
        println it.path
    }
}
  • Using the list was taken from the IDEA above. The problem with the above scripts is that they require to import groovy.io.FileType.FILES. gradle scripts don't like that. So I just made a method to look for the files that calls itself when a directory is found. – Timothy Strunk Mar 19 '20 at 21:07
2

With Kotlin Gradle script one can do it like this:

// ...
val yamls = layout.files({
    file("src/main/resources/mixcr_presets").walk()
        .filter { it.extension == "yaml" }
        .toList()
})
// ...
dbolotin
  • 390
  • 3
  • 12