lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

 

this is just a wrapper around find. Very basic, too, but then it is also short :)

 

function findfile(where)
    local t = {}
    local p

    local iterator = coroutine.wrap(function ()
        local l
        for l in p:lines() do
            coroutine.yield(l)
        end
    end)

    return setmetatable(t, {
        __index = function(_, key)
            return function(_, val)
                if val == nil then val = '' end
                t[key] = val
                return t
            end
        end,
        __call = function()
            local cmd = '/usr/bin/find ' .. where
            local k, v
            for k, v in pairs(t) do
                cmd = cmd .. ' -' .. k
                if v ~= '' then cmd = cmd .. ' "' .. tostring(v) .. '"' end
            end
            p = io.popen(cmd, "r")
            return iterator
        end
    })
end

 

-- example:
for item in findfile ("."):type("f"):name("*Font*")() do
    print(">" .. item .. "<")
end

Gunnar 

 


Natanael Copa <natanael.copa@gmail.com> hat am 16. Dezember 2011 um 11:03 geschrieben:

> On Fri, Dec 16, 2011 at 10:46 AM, Xavier Wang <weasley.wx@gmail.com> wrote:
> > Hi my dear list :-)
> >
> > I have try to find a lua module that acts like GNU find, it can
> > enumerate files in a directory, and makes some filter on it:
> >
> > find "." :name ".svn" :prune()
> >    :OR():print()
> >
> >
> > or something like it.
> >
> > Has someone heard it somewhere?
>
> No, but I have been looking for exactly the same.
> This might be of interest:
> http://lua-users.org/wiki/DirTreeIterator
>
> I have been thinking in the lines of:
> find(startdir, maxdepth, function(file)
>    -- do something with file
> end)
>
> but your API looks nicer.
>
> Would be awesome if something like that could be added to lfs or
> penlight or added to its own library.
>
> Thanks!
>
> --
> Natanael Copa
>