How can one retrieve all filenames under a folder in Lua?

You can use the lfs library in Lua to get all the file names in a folder. First, make sure the lfs library is installed. Then, you can use the following code to get all the file names in the folder:

lfs = require("lfs")

function getFiles(path)
    local files = {}
    for file in lfs.dir(path) do
        if file ~= "." and file ~= ".." then
            local filePath = path .. "/" .. file
            local attr = lfs.attributes(filePath)
            if attr.mode == "file" then
                table.insert(files, file)
            end
        end
    end
    return files
end

local files = getFiles("文件夹路径")
for _, file in ipairs(files) do
    print(file)
end

Please make sure to replace “folder path” with the folder path that you want to retrieve the file names from. This code will return a list containing the file names and print them out one by one.

bannerAds