Directory Listing

To list a directory in LUA you seem to be best off using some sort of external library, capable of dealing with both UNIX and Windows. I’ve found LuaFileSystem but I don’t know how to compile it and if the distribution will work with any OS.

Is there an easy solution? Maybe someone with more knowledge could make a small pack and example for all to benefit from? :)

(What I’m trying to make is a context menu generated from a directory, loading dsps and eventually xrnt)

local dir = os.currentdir()  
local platform = os.platform()  
if (platform == 'WINDOWS') then  
 -- special handling for windows  
elseif (platform == 'MACINTOSH') then  
 -- special handling for mac  
elseif (platform == 'LINUX') then  
 -- special handling for linux  
local extension = '*.*'  
local files = os.filenames(dir, extension)  

Thanks. I guess I have to investigate the different outputs of ls and dir. It’ll be a bit difficult when only on Windows though.

No, you don’t. You can just use the os.filenames function, like dblue showed above.

Let’s say I’m on Windows, and I have a directory at C:\Users\Foo\Documents\ that contains the following files:


then this code:

for _,filename in pairs(os.filenames("C:/Users/Foo/Documents/")) do  

would output


to the terminal, and

for _,filename in pairs(os.filenames("C:/Users/Foo/Documents/"), "*.txt") do  

would just output


The same goes for other platforms - if you’re on Linux, and you want to look for files in the directory /Users/foo/importantfiles/, just use


The documentation for this function is in Lua.Standard.API.lua. Note that os.filenames is not part of the actual Lua standard library, but was added “under the hood” of Renoise for scripting. If you were just using Lua in general (i.e. not for scripting Renoise) then an external library would be the best way to do things like list files. Luckily, the Renoise dev team added some useful functions like this one.

Thanks a lot cocoa. I obviously missed it.