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库,这个库可以实现平台无关的文件系统访问。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM