xls*_*son 86
这应该可以解决您的问题:
import static groovy.io.FileType.FILES
new File('.').eachFileRecurse(FILES) {
if(it.name.endsWith('.groovy')) {
println it
}
}
Run Code Online (Sandbox Code Playgroud)
eachFileRecurse
采用枚举FileType,指定您只对文件感兴趣.通过过滤文件名可以轻松解决问题的其余部分.可能值得一提的是,eachFileRecurse
通常eachDirRecurse
只对文件和文件夹进行递归,同时只查找文件夹.
Tou*_*umi 16
groovy版本2.4.7:
new File(pathToFolder).traverse(type: groovy.io.FileType.FILES) { it ->
println it
}
Run Code Online (Sandbox Code Playgroud)
你也可以添加像过滤器一样
new File(parentPath).traverse(type: groovy.io.FileType.FILES, nameFilter: ~/patternRegex/) { it ->
println it
}
Run Code Online (Sandbox Code Playgroud)
// Define closure
def result
findTxtFileClos = {
it.eachDir(findTxtFileClos);
it.eachFileMatch(~/.*.txt/) {file ->
result += "${file.absolutePath}\n"
}
}
// Apply closure
findTxtFileClos(new File("."))
println result
Run Code Online (Sandbox Code Playgroud)