File io 使用文件结构创建递归表

File io 使用文件结构创建递归表,file-io,recursion,io,lua,lua-table,File Io,Recursion,Io,Lua,Lua Table,我想从表中的文件夹递归获取文件列表 我尝试了以下方法: function getStructure ( path ) local fileArray = readFilesAndFoldersFromPath( path ) -- returns table for i = 1, #fileArray do if fileArray[i].type == 'folder' then getStructure ( fileArray[i].p

我想从表中的文件夹递归获取文件列表

我尝试了以下方法:

function getStructure ( path )
    local fileArray = readFilesAndFoldersFromPath( path ) -- returns table
    for i = 1, #fileArray do
        if fileArray[i].type == 'folder' then
            getStructure ( fileArray[i].path )
        end
    end
end
getStructure( '/folder/example')
这很有效。但现在我还希望结果出现在一个多维表中,如下所示:

[1] =>  file1
    =>  file2
[2] =>  folder1
        => file 3
        => file 4
        => folder 2
[3] =>  file 5
如何做到这一点

function getStructure ( path )
    local fileArray = readFilesAndFoldersFromPath( path ) -- returns table
    for i = 1, #fileArray do
        if fileArray[i].type == 'folder' then
          fileArray[i].folder_content =  getStructure ( fileArray[i].path )
        end
    end
    return fileArray
end

local myFolderStructure = getStructure( '/folder/example')