lua lfs库的使用——递归获取子文件路径

require "io"
require "lfs"

----------------------------------------------------------------------------------
--It will return a table that contents all the file paths in the rootpath
function getpathes(rootpath, pathes)
    pathes = pathes or {}
    for entry in lfs.dir(rootpath) do
        if entry ~= '.' and entry ~= '..' then
            local path = rootpath .. '\\' .. entry
            local attr = lfs.attributes(path)
            assert(type(attr) == 'table')
            
            if attr.mode == 'directory' then
                getpathes(path, pathes)
            else
                table.insert(pathes, path)
            end
        end
    end
    return pathes
end

传入一个根目录路径,递归该路径下的所有子目录,返回所有文件全路径。

用到了lua的lfs库,这个库可以实现平台无关的文件系统访问。

posted @ 2012-05-10 17:33  MeT  阅读(7652)  评论(1)    收藏  举报