获取目录中所有文件的列表(递归) [英] Get a list of all the files in a directory (recursive)
本文介绍了获取目录中所有文件的列表(递归)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试过了:
我试图获得(不打印,很容易)目录中的文件列表,它是子目录。 p>
I've tried:
def folder = "C:\\DevEnv\\Projects\\Generic";
def baseDir = new File(folder);
files = baseDir.listFiles();
我只得到dirs。我也试过
I only get the dirs. 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.
如何获取列表?
推荐答案
此代码适用于我:
import groovy.io.FileType
def list = []
def dir = new File("path_to_parent_dir")
dir.eachFileRecurse (FileType.FILES) { file ->
list << file
}
之后,list变量包含所有文件(java.io.File)给定的目录及其子目录:
Afterwards the list variable contains all files (java.io.File) of the given directory and its subdirectories:
list.each {
println it.path
}
这篇关于获取目录中所有文件的列表(递归)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文