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庫,這個庫可以實現平臺無關的文件系統訪問。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章