发件人: "p. shkadzko";<p.shkadzko@gmail.com>;
发送时间: 2017年2月22日(星期三) 上午8:36
收件人: "lua-l"<lua-l@lists.lua.org>;
主题: list files in dir recursively
Hi guys,
Here is a function that finds all files in nested dirs and returns a table of file paths. I can't figure out how to keep the table variable inside the function. For example.
require 'paths'
require 'lfs'
fpaths = {}
function isdir(path)
return lfs.attributes(path)["mode"] == "directory"
end
function listfiles(dir)
local files = paths.dir(dir)
for i=1,#files do
if files[i] ~= '.' and files[i] ~= '..' then
next_dir = dir..'/'..files[i]
if isdir(next_dir) then
listfiles(next_dir)
else
table.insert(fpaths,files[i])
end
end
end
return fpaths
end
If I include fpaths inside listfiles it will be overwritten with each new recursive call. I tried to create a closure but I think I just do not understand how they work in lua.
Can somebody help me out with this?
Best,
Pavel