如何检查目录是否包含groovy中的文件

问题描述 投票:0回答:1

我有一个包含大量文件的案例或目录,我想要一个 groovy 中的命令或一个方法,可以检查我的目录中名称中包含字母“h”的所有文件,并返回这些文件的列表名字

def findFilesWithModulesH(def steps, def target) {
    def filesWithModulesH = []
    new File(target).eachFile { file ->
        steps.println "AAAAAAAAAAAAAAAA0 ${file}"
        def fileLabel = file.name.tokenize("-")[0].toUpperCase()

>    if(!filesWithModulesH.contains(fileWithModulesH)){
>                 filesWithModulesH.add(fileWithModulesH)
>             }
>         }
>     }
file jenkins groovy jenkins-groovy
1个回答
0
投票

有不同的方法可以做到这一点。

方法01:使用

findFiles
Jenkins实用函数


def folder = 'folder/path'
def fileNameList = []
dir(folder) {
    def fileList = findFiles(glob: '*h*')
    fileList.each { fileNameList.add(it.name) }
    
}
println fileNameList

方法2:使用Groovy

注意函数中的

@NonCPS
注释

@NonCPS
def getFiles(def path) {
                    
    def filesWithH = []
    new File(path).traverse(type: groovy.io.FileType.FILES, maxDepth: 0) { file ->
        if (file.name.contains('h')) {
            filesWithH << file.name
        }
    }
    return filesWithH
}
© www.soinside.com 2019 - 2024. All rights reserved.