要想不使用Lua的lfs库,唯一的办法就是使用操作系统提供的命令
首先定义一个遍历目录的函数:
local function traverseFiles(dir)
local cmd = string.format('dir "%s" /b /s', dir)
local handle = io.popen(cmd, 'r')
local result = handle:read("*a")
handle:close()
local files = {}
for file in result:gmatch("[^\r\n]+") do
if not file:match("^[.]$") and not file:match("^[.][.]$") then
table.insert(files, file)
end
end
return files
end
倘若使用了别人封装的lua引擎,对read函数做了防溢出操作,不允许进行read(“*a”)操作时,可以做如下尝试
local function traverseFiles(dir)
local cmd = string.format('dir "%s" /b /s', dir)
local handle = io.popen(cmd, 'r')
if(handle == nil) then
log_w("open file for dir fail")
return
end
local files = {}
for file in handle:lines() do
if not file:match("^[.]$") and not file:match("^[.][.]$") then
table.insert(files, file)
end
end
handle:close()
return files
end
返回的files便可以为所欲为了
for i, file in ipairs(cachefiles) do
os.remove(file)
end