lua遍历文件

主要是利用这个luaforwindows的这个软件来执行下面这些代码,把遍历目录得到的每一个路径都存在一个table里面,然后遍历这个table就能把每条目录都打印出来



--dofile("c:\\Users\\Amber\\Desktop\\a.lua")

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 = {}

pathes = getpathes("c:/Users/Amber/Desktop/src", pathes)

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


展开阅读全文

没有更多推荐了,返回首页