lua: list files in a directory

User avatar
domtron vox
Member
 
Posts: 111
Joined: Thu Feb 20, 2014 21:07
GitHub: DomtronVox
IRC: Domtron
In-game: Domtron

lua: list files in a directory

by domtron vox » Fri Mar 28, 2014 12:29

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.
 

User avatar
Krock
Developer
 
Posts: 4412
Joined: Thu Oct 03, 2013 07:48
Location: Switzerland
GitHub: SmallJoker

by Krock » Fri Mar 28, 2014 12:31

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.
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>
 

Sokomine
Member
 
Posts: 3918
Joined: Sun Sep 09, 2012 17:31
GitHub: Sokomine

by Sokomine » Fri Mar 28, 2014 13:50

Most mods I've seen which require such a functionality use diffrent approaches based on operating system. See for example towntest.
A list of my mods can be found here.
 

User avatar
ShadowNinja
Developer
 
Posts: 199
Joined: Tue Jan 22, 2013 22:35
GitHub: ShadowNinja
IRC: ShadowNinja
In-game: ShadowNinja

by ShadowNinja » Fri Mar 28, 2014 15:50

You can depend on LuaFileSystem and use the dir iterator.
For example:
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
Last edited by ShadowNinja on Fri Mar 28, 2014 15:52, edited 1 time in total.
The best way to contact me is usually IRC (InchraNet, freenode).
 

User avatar
domtron vox
Member
 
Posts: 111
Joined: Thu Feb 20, 2014 21:07
GitHub: DomtronVox
IRC: Domtron
In-game: Domtron

by domtron vox » Fri Mar 28, 2014 19:03

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:
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.
Last edited by domtron vox on Fri Mar 28, 2014 19:14, edited 1 time in total.
 

User avatar
ShadowNinja
Developer
 
Posts: 199
Joined: Tue Jan 22, 2013 22:35
GitHub: ShadowNinja
IRC: ShadowNinja
In-game: ShadowNinja

by ShadowNinja » Fri Mar 28, 2014 22:12

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:
Code: Select all
package.cpath = package.cpath
                .. ";" .. modpath .. "/lib?.so"
                .. ";" .. modpath .. "/?.dll"

After the modpath fetching but before the require() call.
The best way to contact me is usually IRC (InchraNet, freenode).
 


Return to WIP Mods



Who is online

Users browsing this forum: MSN [Bot], ThorfinnS and 4 guests