rwen
------------------ 原始邮件 ------------------
发件人: "R.wen";<rwen2012@qq.com>;
发送时间: 2017年2月22日(星期三) 上午9:07
收件人: "Lua mailing list"<lua-l@lists.lua.org>;
主题: 回复:list files in dir recursively
how about this version?
local function listfile(dir, fpl)
for entry in lfs.dir(dir) do
if entry ~= "." and entry ~= ".." then
local ne = dir..'\\'..entry
if lfs.attributes(ne).mode == "directory" then
listfile(ne, fpl)
else --file
fpl[#fpl + 1] = ne
end
end
end
end
local fpl = {}
listfile(dir, fpl)
for _, n in pairs(fpl) do
print(n)
end
------------------ 原始邮件 ------------------
发件人: "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