在Lua中,可以使用lfs(Lua File System)库来遍历文件夹并获取文件名。下面是一个例子:
lfs = require("lfs")
function traverseFolder(path)
for file in lfs.dir(path) do
if file ~= "." and file ~= ".." then
local filePath = path .. "/" .. file
local attr = lfs.attributes(filePath)
if attr.mode == "directory" then
traverseFolder(filePath) -- 递归遍历子文件夹
else
print(file) -- 打印文件名
end
end
end
end
traverseFolder("path/to/folder")
在此示例中,traverseFolder函数接收一个文件夹路径作为参数,使用lfs.dir遍历文件夹中的文件和子文件夹。对于每个文件,如果它是一个文件夹,则递归调用traverseFolder函数;否则,打印文件名。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: Lua中的垃圾回收机制是什么