【问题标题】:Get a list of all the files in a directory (recursive)获取目录中所有文件的列表(递归)
【发布时间】:2011-04-26 14:53:49
【问题描述】:

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

我试过了:

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);

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

如何获取列表?

【问题讨论】:

    标签: file groovy directory-listing


    【解决方案1】:

    这是我想出的 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
        }
    }
    

    【讨论】:

    • 使用列表取自上面的IDEA。上述脚本的问题是它们需要导入 groovy.io.FileType.FILES。 gradle 脚本不喜欢这样。所以我只是做了一个方法来查找找到目录时调用自己的文件。
    【解决方案2】:

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

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

    【讨论】:

    • 虽然这可能不会通过子目录递归。但是:为了我的目的,分离出 proguard 文件并一次性导入它们:)
    • 不幸的是,这并没有回答“目录中的所有文件(递归)”的问题。它只会列出当前目录,并且在上下文中具有误导性。
    • fileTree 递归。
    • FileTree 不包括目录(不将它们视为文件)。
    【解决方案3】:

    较新版本的 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

    【讨论】:

      【解决方案4】:

      此代码适用于我:

      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
      }
      

      【讨论】:

      • 默认情况下,groovy 会导入 java.io 而不是 groovy.io,因此要使用 FileType,您必须显式导入它。
      • 对于使用 FileType,请确保使用正确的 groovy 版本:“类 groovy.io.FileType 是在 Groovy 版本 1.7.1 中引入的。”见:stackoverflow.com/questions/6317373/…
      • 这是显示文件夹名称及其路径。例如:/tmp/directory1 如何在输出中单独获取directory1
      • 奇怪..这给出了根路径,即使我用.开头它也是/./path
      • 如何列出目录中的所有文件夹?
      猜你喜欢
      • 2017-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-19
      • 2010-12-01
      • 2020-01-31
      • 1970-01-01
      相关资源
      最近更新 更多