File listing in Darktable with LUA

Hi,

I’d like to write a LUA script to DT what has a function to return with the list of files in a specific directory.
My problem is that if I run the script in a debugger (ZeroBrane studio) it works well, but DT won’t load the script.
If I remove the “lfs = require “lfs”” part DT loads it, but of course the function doesn’t works.
How can I use the “lfs” in DT?

lfs = require "lfs"
local myFiles = {}

local function ListFiles(directory)
  local dirFiles = {}
  for file in lfs.dir(directory) do
        if file ~= "." and file ~= ".." then
          print("1file in dir: "..file)
          table.insert(dirFiles, file)
       end
  end
  return dirFiles
end

-- example usage
myFiles = ListFiles("D:\\Downloads\\")

Thank you

Hi,

as far as I remember you need to use the dt lua api for file management dtutils.file. The manual here might help:

https://docs.darktable.org/lua/stable/lua.scripts.api.manual/dtutils.file/details/

On a linux system, you also will find scripts in three different folders (examples, official and contrib) under ~/.config/darktable/lua/ which give a good insight.

1 Like

Use io.popen("dir")

1 Like

Thank you all, io.popen("dir") works.


local function ListFilesPopen2(directory)
  local fileList = {}
    for fileName in io.popen('dir "'..directory..'" /b'):lines() do
        table.insert(fileList, fileName)
    end
    return fileList
end
1 Like