首页 > 其他 > 详细

lua遍历文件

时间:2014-07-05 22:07:21      阅读:542      评论:0      收藏:0      [点我收藏+]

看了不少人的,主要还是错误处理有点问题,不多说了

贴代码:

require "lfs"


function getpathes(rootpath, pathes)
    pathes = pathes or {}

    ret, files, iter = pcall(lfs.dir, rootpath)
    if ret == false then
        return pathes
    end
    for entry in files, iter do
        local next = false
        if entry ~= . and entry ~= .. then
            local path = rootpath .. / .. entry
            local attr = lfs.attributes(path)
            if attr == nil then
                next = true
            end

            if next == false then 
                if attr.mode == directory then
                    getpathes(path, pathes)
                else
                    table.insert(pathes, path)
                end
            end
        end
        next = false
    end
    return pathes
end

pathes = {}

getpathes("/", pathes)

for key, path in pairs(pathes) do
    print(key .. " " .. path)
end

 

lua遍历文件,布布扣,bubuko.com

lua遍历文件

原文:http://www.cnblogs.com/zelos/p/3826225.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!