How to resolve the algorithm Walk a directory/Recursively step by step in the Lua programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Walk a directory/Recursively step by step in the Lua programming language

Table of Contents

Problem Statement

Walk a given directory tree and print files matching a given pattern.

Note: This task is for recursive methods.   These tasks should read an entire directory tree, not a single directory.

Note: Please be careful when running any code examples found here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Walk a directory/Recursively step by step in the Lua programming language

Source code in the lua programming language

local lfs = require("lfs")

-- This function takes two arguments:
-- - the directory to walk recursively;
-- - an optional function that takes a file name as argument, and returns a boolean.
function find(self, fn)
  return coroutine.wrap(function()
    for f in lfs.dir(self) do
      if f ~= "." and f ~= ".." then
        local _f = self .. "/" .. f
        if not fn or fn(_f) then
          coroutine.yield(_f)
        end
        if lfs.attributes(_f, "mode") == "directory" then
          for n in find(_f, fn) do
            coroutine.yield(n)
          end
        end
      end
    end
  end)
end

-- Examples
-- List all files and directories
for f in find("directory") do
  print(f)
end

-- List lua files
for f in find("directory", function(self) return self:match("%.lua$") end) do
  print(f)
end

-- List directories
for f in find("directory", function(self) return "directory" == lfs.attributes(self, "mode") end) do
  print(f)
end


-- Gets the output of given program as string
-- Note that io.popen is not available on all platforms
local function getOutput(prog)
    local file = assert(io.popen(prog, "r"))
    local output = assert(file:read("*a"))
    file:close()
    return output
end

-- Iterates files in given directory
local function files(directory, recursively)
    -- Use windows" dir command
    local directory = directory:gsub("/", "\\")
    local filenames = getOutput(string.format("dir %s %s/B/A:A", directory, recursively and '/S' or ''))
    
    -- Function to be called in "for filename in files(directory)"
    return coroutine.wrap(function()
        for filename in filenames:gmatch("([^\r\n]+)") do
            coroutine.yield(filename)
        end    
    end)
end

-- Walk "C:/Windows" looking for executables
local directory = "C:/Windows"
local pattern = ".*%.exe$" -- for finding executables
for filename in files(directory, true) do
    if filename:match(pattern) then
        print(filename)
    end
end


  

You may also check:How to resolve the algorithm Ackermann function step by step in the E programming language
You may also check:How to resolve the algorithm Arithmetic/Complex step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Odd word problem step by step in the Java programming language
You may also check:How to resolve the algorithm Bin given limits step by step in the jq programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the FutureBasic programming language