创建具有文件结构的递归表

时间:2014-11-01 17:46:12

标签: 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].path )
        end
    end
end
getStructure( '/folder/example')

哪个有效。但是现在我也希望将结果放在像这样的多维表中:

[1] =>  file1
    =>  file2
[2] =>  folder1
        => file 3
        => file 4
        => folder 2
[3] =>  file 5

怎么做?

1 个答案:

答案 0 :(得分:0)

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')