2014-11-01 61 views
0

我想從表中遞歸地獲取文件夾中的文件列表。使用文件結構創建遞歸表

我試過如下:

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 

如何做到這一點?

回答

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