Dir Tree Iterator

Send Snippet To: Save this snippet to Code Collector Pro -- view all roger's snippets
language: Other
license: Other

Code for Snippet:

                
 
It depends on the LuaFileSystem library ("lfs") [1].
 
-- code by AlexanderMarinov
-- Compatible with Lua 5.1 (not 5.0).
 
-- Code by David Kastrup
require "lfs"
 
function dirtree(dir)
  assert(dir and dir ~= "", "directory parameter is missing or empty")
  if string.sub(dir, -1) == "/" then
    dir=string.sub(dir, 1, -2)
  end
 
  local function yieldtree(dir)
    for entry in lfs.dir(dir) do
      if entry ~= "." and entry ~= ".." then
        entry=dir.."/"..entry
	local attr=lfs.attributes(entry)
	coroutine.yield(entry,attr)
	if attr.mode == "directory" then
	  yieldtree(entry)
	end
      end
    end
  end
 
  return coroutine.wrap(function() yieldtree(dir) end)
end
 
comments powered by Disqus

Info

Link to this snippet:


Download to Code Collector

To use the direct link to your snippet on CodeCollector.net either copy the html from the above section or drag the Download to Code Collector to where you would like to use it.

More Info:

Times Viewed: 7
Date Added: 2018-01-09 04:53:31
Last Modified: 2018-01-10 09:54:13

Web Analytics