从Lua目录获取文件名

我需要从 Lua 中的一个目录中获取文件名。

我不想使用 require "lfs"

使用 popen / open 会很有帮助。

原文链接 https://stackoverflow.com/questions/5585571

点赞
stackoverflow用户632566
stackoverflow用户632566

使用Lua Wiki中的 Shell 函数。作为一个命令(c),传递一个"ls /path/ pattern"(假设你在 Unix 或者在运行 Windows 时安装了 Cygwin)。

2011-04-07 19:37:07
stackoverflow用户596285
stackoverflow用户596285

请参见 lua-list 中的这篇文章

具体可以修改以下代码以实现所需功能:

local dircmd = "find . -type f -print" -- 默认使用 Unix
if string.sub(package.config,1,1) == '\\' then
        -- Windows
        dircmd = "dir /b/s"
end

os.execute(dircmd .. " > zzfiles")

local luafiles = {}
for f in io.lines("zzfiles") do
        if f:sub(-4) == ".lua" then
                luafiles[#luafiles+1] = f
        end
end

print(table.concat(luafiles, "\n"))
2011-04-07 19:57:35