How to recursively find all files in a directory in DolphinDB?

Issue

The DolphinDB function files can only list all files under the current directory, and the filename is not the full path. How can I list all files with .gz extension in the directory just like Linux find command?

[[email protected] ~]$ find ./ -name "*.gz"
./cmake-3.12.3.tar.gz
./mongoose/test/data/hello.txt.gz

Solution

You can define a function like this:

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
}

For example:

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

output:

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

Answered By – dontyousee

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published