获取目录中所有文件的列表(递归)

发布于 2024-09-27 19:27:40 字数 478 浏览 3 评论 0原文

我正在尝试获取(不是打印,这很容易)目录及其子目录中的文件列表。

我尝试过:

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

我只获取目录。我也尝试过:

def files = [];

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

baseDir.eachFileRecurse(FileType.FILES, processFileClosure);

但是“文件”在闭包范围内未被识别。

我如何获取列表?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夏末染殇 2024-10-04 19:27:40

此代码对我有用:

import groovy.io.FileType

def list = []

def dir = new File("path_to_parent_dir")
dir.eachFileRecurse (FileType.FILES) { file ->
  list << file
}

之后列表变量包含给定目录及其子目录的所有文件(java.io.File):

list.each {
  println it.path
}

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
}
来日方长 2024-10-04 19:27:40

较新版本的 Groovy (1.7.2+) 提供了 JDK 扩展,可以更轻松地遍历目录中的文件,例如:

import static groovy.io.FileType.FILES
def dir = new File(".");
def files = [];
dir.traverse(type: FILES, maxDepth: 0) { files.add(it) };

另请参阅 [1] 了解更多示例。

[1] http://mrhaki.blogspot.nl/2010/ 04/groovy-goodness-traversing-directory.html

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

硪扪都還晓 2024-10-04 19:27:40

以下内容适用于 Android 项目的 Gradle / Groovy for build.gradle,而无需导入 groovy.io.FileType (注意:不会递归子目录,但当我找到这个解决方案时,我不再关心递归,所以你也可能不关心):

FileCollection proGuardFileCollection = files { file('./proguard').listFiles() }
proGuardFileCollection.each {
    println "Proguard file located and processed: " + it
}

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
}
千鲤 2024-10-04 19:27:40

这是我为 gradle 构建脚本想到的:

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
    }
}

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
    }
}
原来是傀儡 2024-10-04 19:27:40

使用 Kotlin Gradle 脚本,可以这样做:

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

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()
})
// ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文