读取文件夹中的文件,然后按 groovy 执行某些操作

Read files in folder and then do something by groovy

我写了一个脚本来读取文件,然后从指定的路径做一些事情:

def file = new File(/"a.txt"/)
def s = []
s = file.filterLine  { it.contains("project ")}
def array = []
def a = []
array << s.toString().split(/(<|=|:|"|,|\/>)/)
a = array.find{ it.contains("SYN_3-1_M5_integration")}
b = a.findAll { it.startsWith("SYN_3")}
println b.unique()

我只是想问一下如果我有很多txt文件如何使用上面的代码。我是 Groovy 的新手。提前致谢!

下面的一段代码应该可以完成这项工作:

import groovy.io.FileType

new File('PATH_TO_FOLDER').eachFile(FileType.FILES) { file ->
    def s = []
    s = file.filterLine  { it.contains("project ")} 
    def array = []
    def a = []
    array << s.toString().split(/(<|=|:|"|,|\/>)/)
    println array.toString() 
    a = array.find{ it.contains("SYN_3-1_M5_integration")} 
    println a
    b = a.findAll { it.startsWith("SYN_3")} 
    println b.unique()
}

基本上它会遍历 PATH_TO_FOLDER 指定的文件夹中的每个文件,并按照发布的方式进行处理。