Re: How to get list of output of find command of Linux
- Posted by petelomax Jul 18, 2017
- 1928 views
I quickly tried that code on Phix, and apart from the parameter default of current_dir() not being supported on Phix (really must fix that some day), and wildcard_match(), it works fine.
One of the reasons for doing so was that I thought of a slightly better way to manage the queue (some comments stripped for clarity):
function get_files(sequence path="", sequence wildname="*", integer maxdepth=0) -- -- path : directory path to scan -- wildname : wildcard pattern to look for -- maxdepth : maximum folder depth to scan within path -- path = iff(path=""?current_dir():get_proper_path(path)) integer depth = 1 sequence files = {}, queue = {path,depth,{}} while length(queue) do {path,depth,queue} = queue object d = dir(path) if sequence(d) then for i=1 to length(d) do string {name,attr} = d[i] if not find(name,{".",".."}) then string full_path = join_path({path,name}) if find('d',attr) then if maxdepth=0 or depth<maxdepth then queue = {full_path,depth+1,queue} end if elsif wildcard_match(wildname, name) then files = append(files, full_path) end if end if end for end if end while return files end function
Not at all sure that will work on OE, and I can accept that a "flat" queue might be slightly more efficient, but I was aiming for elegance.
Pete
PS: On recursion vs iteration, Greg may have slightly exaggerated, but is essentially correct. An iterative solution will create just one queue and plant each element in the result sequence files just once, and will therefore always be slightly more efficient than a recursive solution, but any difference in performance is probably insignificant. In the end, whichever you find easier (recursion vs iteration), to understand and maintain, is the one to use.