Groovy 中匹配特定文件类型的所有文件的递归列表 [英] Recursive listing of all files matching a certain filetype in Groovy

查看:29
本文介绍了Groovy 中匹配特定文件类型的所有文件的递归列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 Groovy 中递归列出与特定文件类型匹配的所有文件.这个例子几乎做到了.但是,它不会列出根文件夹中的文件.有没有办法修改它以列出根文件夹中的文件?或者,有什么不同的方法吗?

I am trying to recursively list all files that match a particular file type in Groovy. This example almost does it. However, it does not list the files in the root folder. Is there a way to modify this to list files in the root folder? Or, is there a different way to do it?

推荐答案

这应该可以解决您的问题:

This should solve your problem:

import static groovy.io.FileType.FILES

new File('.').eachFileRecurse(FILES) {
    if(it.name.endsWith('.groovy')) {
        println it
    }
}

eachFileRecurse 采用枚举 FileType,指定您只对文件感兴趣.剩下的问题很容易通过过滤文件名来解决.可能值得一提的是,eachFileRecurse 通常对文件和文件夹进行递归,而 eachDirRecurse 只查找文件夹.

eachFileRecurse takes an enum FileType that specifies that you are only interested in files. The rest of the problem is easily solved by filtering on the name of the file. Might be worth mentioning that eachFileRecurse normally recurses over both files and folders while eachDirRecurse only finds folders.

这篇关于Groovy 中匹配特定文件类型的所有文件的递归列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆