Advertisement
BombBloke

fs.find (ComputerCraft)

Apr 14th, 2015
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.96 KB | None | 0 0
  1. fs.find = function(path)
  2.     local pathParts, results, curfolder = {}, {}, "/"
  3.     for part in path:gmatch("[^/]+") do pathParts[#pathParts + 1] = part:gsub("*", "[^/]*") end
  4.     if #pathParts == 0 then return {} end
  5.    
  6.     local prospects = fs.list(curfolder)
  7.     for i = 1, #prospects do prospects[i] = {["parent"] = curfolder, ["depth"] = 1, ["name"] = prospects[i]} end
  8.    
  9.     while #prospects > 0 do
  10.         local thisProspect = table.remove(prospects, 1)
  11.         local fullPath = fs.combine(thisProspect.parent, thisProspect.name)
  12.        
  13.         if thisProspect.name == thisProspect.name:match(pathParts[thisProspect.depth]) then
  14.             if thisProspect.depth == #pathParts then
  15.                 results[#results + 1] = fullPath
  16.             elseif fs.isDir(fullPath) and thisProspect.depth < #pathParts then
  17.                 local newList = fs.list(fullPath)
  18.                 for i = 1, #newList do prospects[#prospects + 1] = {["parent"] = fullPath, ["depth"] = thisProspect.depth + 1, ["name"] = newList[i]} end
  19.             end
  20.         end
  21.     end
  22.    
  23.     return results
  24. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement