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