首页 > 代码库 > lua遍历文件目录
lua遍历文件目录
用lua遍历文件目录,收集特定类型的文件:
1 local LINUX = "linux" 2 local WIN = "win" 3 local platform = WIN 4 5 local need_the_filetype = function(tfiletype, filename) 6 for k, v in pairs(tfiletype) do 7 if (v == ".") or (v == ".*") then 8 return true 9 end10 11 local from, to = string.find(filename, "%" .. v)12 while from do13 local f, t = string.find(filename, "%" .. v, to)14 if not(f) then15 break16 end17 from, to = f, t18 end19 20 if from and (to == string.len(filename)) then21 return true22 end23 end24 return false25 end26 27 local function scan_dir_file(path, tfiletype, tfilename)28 path = path or "./"29 tfiletype = tfiletype or {"."}30 tfilename = tfilename or {}31 32 local cmd = "ls " .. path33 if (platform == WIN) then34 cmd = ‘dir "‘ .. path .. ‘" /b‘35 end36 local popen = io.popen(cmd)37 if popen then38 for filename in popen:lines() do39 if (string.find(filename, "%.")) then40 if need_the_filetype(tfiletype, filename) then41 tfilename[#tfilename + 1] = {path = path, name = filename,}42 end43 else44 scan_dir_file(path .. filename .. "/", tfiletype, tfilename)45 end46 end47 end48 return tfilename49 end50 51 ---------------------------------52 -- interface53 --54 scan = {55 walk = function(path, tfiletype, tfilename)56 return scan_dir_file(path, tfiletype, tfilename)57 end,58 }59 60 --------------------------------61 -- test62 --63 local t = scan.walk()64 for k, v in pairs(t) do65 print(k, v.path, v.name)66 end
lua遍历文件目录
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。