首页 > 代码库 > lua遍历目录

lua遍历目录

require"lfs"
function findindir (path, wefind, r_table, intofolder)
    for file in lfs.dir(path) do
        if file ~= "." and file ~= ".." then
            local f = path..'\\'..file
            --print ("/t "..f)
            if string.find(f, wefind) ~= nil then
                --print("/t "..f)
                table.insert(r_table, f)
            end
            local attr = lfs.attributes (f)
            assert (type(attr) == "table")
            if attr.mode == "directory" and intofolder then
                findindir (f, wefind, r_table, intofolder)
            else
                --for name, value in pairs(attr) do
                --    print (name, value)
                --end
            end
        end
    end
end
local currentFolder = [[C:\]]
-------------------------------------
local input_table = {}
findindir(currentFolder, "%.txt", input_table, false)--查找txt文件
i=1
while input_table[i]~=nil do
print(input_table[i])
i=i+1
end

lua遍历目录