ITERATING OVER DIRECTORIES

Actions on files may depend on the file's type. It makes sense to represent such actions as tables, indexed by filetypes, of functions which take as argument the pathname of the file. Here we define a function recurse of such an action table, act and a directory d, that applies act recursively on subdirectories.

local dir in riscos
local sep = "%s.%s"
local recurse = \ (act, folder)
  local dir_action -- forward declaration
  dir_action = \ (d)
    for leaf, ftype in dir (d) do
      local proc = act[ftype]
      local obj = sep:format (d,leaf)
      if proc then proc (obj) end -- if
    end -- for
  end -- function
  act[0x1000] = dir_action -- directory filetype
  dir_action (folder)
end -- function
Be careful not to have your file-action code alter the contents of a directory while it is being scanned. If you want to delete, create or update files it is best for the file-action code to make a list of the commands required, and then, after the scan, use os.execute actually to do the changes.