码迷,mamicode.com
首页 > 其他好文 > 详细

lua遍历文件

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

标签:style   blog   color   文件   问题   cti   

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

贴代码:

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遍历文件

标签:style   blog   color   文件   问题   cti   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!