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

贴代码:

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

 

相关文章:

  • 2021-05-21
  • 2021-07-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-24
猜你喜欢
  • 2021-12-03
  • 2021-07-05
  • 2021-08-10
  • 2022-12-23
  • 2022-01-30
  • 2022-12-23
  • 2021-12-26
相关资源
相似解决方案