Re: How to get list of output of find command of Linux

new topic     » goto parent     » topic index » view thread      » older message » newer message
petelomax said...

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):

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.

Interesting. You're basically creating a linked list instead of a flat queue. Neat!

petelomax said...

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.

I suppose I like to hyperbolize things once in a while. Simply knowing that a particular method is introducing additional overhead makes it feel inefficient. But in most cases the difference is likely imperceptible.

petelomax said...

Maybe something like this? http://rosettacode.org/wiki/Generator/Exponential#Phix (but with only one task)

Sort of? I don't think you'd need to use a task for that though. I was thinking of a recursive version of something like FindFirstFile, which basically uses a cursor to continue generating items until it's reach the end of the list.

petelomax said...
rneu said...

A walk_dir() approach code here will be really illustrative and helpful.

It's pretty simple

... 

For a fuller example, see http://openeuphoria.org/docs/std_filesys.html#_1282_walk_dir

I was offering to adapt my get_files() function to work like walk_dir() which I think is what he was asking for. Here's the code...

include std/filesys.e 
include std/wildcard.e 
 
public function get_files( integer rtn_id, sequence path = current_dir(), sequence name = "*", integer maxdepth = 0 ) 
-- 
-- rtn_id   : routine id to call with each full path 
-- path     : directory path to scan 
-- name     : wildcard pattern to look for 
-- maxdepth : maximum folder depth to scan within path 
-- 
    integer depth, exit_code 
    sequence queue, item, full_path 
    object items 
 
    depth = 1 
    exit_code = 0 
    path = canonical_path( path ) 
 
    -- push initial path into queue 
    queue = { {path,depth} } 
 
    while length( queue ) do 
 
        -- pop an item off the queue 
        item = queue[1] 
        queue = queue[2..$] 
 
        -- get the queue item values 
        path = item[1] 
        depth = item[2] 
 
        -- get the directory items 
        items = dir( path ) 
 
        if atom( items ) then 
            -- error 
            continue 
        end if 
 
        for i = 1 to length( items ) do 
            item = items[i] 
 
            if find( item[D_NAME], {".",".."} ) then 
                -- skip these entries 
                continue 
            end if 
 
            full_path = join_path({ path, item[D_NAME] }) 
 
            if find( 'd', item[D_ATTRIBUTES] ) then 
                -- is a directory 
 
                if maxdepth = 0 or depth < maxdepth then 
                    -- queue this directory 
                    queue = append( queue, {full_path,depth+1} ) 
                end if 
 
            else 
                -- is a file 
 
                if wildcard:is_match( name, item[D_NAME] ) then 
 
                    -- call the user routine 
                    exit_code = call_func( rtn_id, {full_path} ) 
 
                    if exit_code != 0 then 
                        -- time to quit 
                        queue = {} 
                        exit 
                    end if 
 
                end if 
 
            end if 
 
        end for 
 
    end while 
 
    return exit_code 
end function 

And then call the function like you would walk_dir()...

function look_at( sequence full_path ) 
    printf( 1, "%s\n", {full_path} ) 
    return 0 
end function 
 
get_files( routine_id("look_at") ) 

-Greg

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu