linux 如何在DolphinDB中递归地找到一个目录中的所有文件?

mgdq6dx1  于 2022-11-22  发布在  Linux
关注(0)|答案(1)|浏览(103)

DolphinDB函数files只能列出当前目录下的所有文件,文件名不是完整路径,如何像Linux find命令一样列出目录下所有扩展名为.gz的文件?

[xjqian@k8s-2 ~]$ find ./ -name "*.gz"
./cmake-3.12.3.tar.gz
./mongoose/test/data/hello.txt.gz
eanckbw9

eanckbw91#

您可以像这样定义函数:

def findR(path, pattern=NULL){
    t = files(path)
    if (isValid(pattern)){
        t = files(path, pattern)
    }
    res = select * from t where !isDir
    update res set res.filename = path +"/" + res.filename
    for (f in select * from files(path) where !!isDir){
        try {
            subRes =  findR(path+"/"+f.filename, pattern)
            res.append!(subRes)
        } catch(ex){
            print("ex:"+path+"/"+f.filename+ string(f.isDir))
        }
    }
    return res
}

例如:

findR("c:/tmp", pattern="%.log")

输出:

filename            isDir   fileSize    lastAccessed    lastModified
c:/tmp/controller.log   false   543,619,626 2022.05.14T09:09:52.405 2022.05.14T06:04:22.000
c:/tmp/P1-node1.log false   729,729,122 2022.05.14T09:10:05.156 2022.05.14T05:44:13.000
c:/tmp/bd/Output.log    false   44,088          2022.04.03T12:30:56.523 2022.04.03T12:30:56.523

相关问题