Lua traverses the file directory

Source: Internet
Author: User

Use Lua to traverse the file directory and collect specific types of files:

 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 traverses the file directory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.