Page 1 of 1

lua: list files in a directory

PostPosted: Fri Mar 28, 2014 12:29
by domtron vox
Is there a way to list the file names in a directory? I tried to use the function in the second answer at http://stackoverflow.com/questions/5303174/get-list-of-directory-in-a-lua but popen function is not supported. Also, that particular function only works with systems that have 'ls' installed. I also browsed the builtin lua files a bit but, didn't find anything.

Any ideas? If it's not possible I think this would be a good helper function to include in the built-in scripts.

Thanks in advance.

PostPosted: Fri Mar 28, 2014 12:31
by Krock
domtron vox wrote:Is there a way to list the file names in a directory? I tried to use the function in the second answer at http://stackoverflow.com/questions/5303174/get-list-of-directory-in-a-lua but popen function is not supported. Also, that particular function only works with systems that have 'ls' installed. I also browsed the builtin lua files a bit but, didn't find anything.

Any ideas? If it's not possible I think this would be a good helper function to include in the built-in scripts.

Thanks in advance.


In which context do you need that function? Mostly it's enough to store data in one file.

PostPosted: Fri Mar 28, 2014 13:50
by Sokomine
Most mods I've seen which require such a functionality use diffrent approaches based on operating system. See for example towntest.

PostPosted: Fri Mar 28, 2014 15:50
by ShadowNinja
You can depend on LuaFileSystem and use the dir iterator.
For example:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local lfs = require("lfs")
local modpath = minetest.get_modpath(minetest.get_current_modname())

print("List of textures in this mod: ")
for filename in lfs.dir(modpath .. "/textures") do
        if filename ~= "." and filename ~= ".." then
                print("\t" .. filename)
        end
end

PostPosted: Fri Mar 28, 2014 19:03
by domtron vox
Krock wrote:In which context do you need that function? Mostly it's enough to store data in one file.


For configuration, I'm playing around with this mod request and wanted to have a sub-folder of the mod were kits could be stored. For example kitmod/kits/noob.kit defines the kit for the rank "noob."



ShadowNinja wrote:You can depend on LuaFileSystem and use the dir iterator.
For example:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local lfs = require("lfs")
local modpath = minetest.get_modpath(minetest.get_current_modname())

print("List of textures in this mod: ")
for filename in lfs.dir(modpath .. "/textures") do
        if filename ~= "." and filename ~= ".." then
                print("\t" .. filename)
        end
end


I saw that lfs was a solution, but I thought it wouldn't be usable in the context of minetest. Would anyone who installed the mod need to also download and install lfs or can I include it somehow?


@Sokomine: thanks for the link, I'll check it out.



EDIT: Ok the town test code helped a lot.

PostPosted: Fri Mar 28, 2014 22:12
by ShadowNinja
domtron vox wrote:I saw that lfs was a solution, but I thought it wouldn't be usable in the context of minetest. Would anyone who installed the mod need to also download and install lfs or can I include it somehow?

You can pre-compile it and include it by adding:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
package.cpath = package.cpath
                .. ";" .. modpath .. "/lib?.so"
                .. ";" .. modpath .. "/?.dll"

After the modpath fetching but before the require() call.