lua: list files in a directory

User avatar
domtron vox
Member
 
Posts: 106
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
Member
 
Posts: 3598
Joined: Thu Oct 03, 2013 07:48
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.
Newest Win32 builds - Find a mod - All my mods
ALL YOUR DONATION ARE BELONG TO PARAMAT (Please support him and Minetest)
New DuckDuckGo !bang: !mtmod <keyword here>
 

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

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
Member
 
Posts: 194
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:
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
Last edited by ShadowNinja on Fri Mar 28, 2014 15:52, edited 1 time in total.
I play on my Minetest server and VanessaE's.
The best way to contact me is usually IRC (InchraNet, freenode).
 

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

User avatar
ShadowNinja
Member
 
Posts: 194
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:
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.
I play on my Minetest server and VanessaE's.
The best way to contact me is usually IRC (InchraNet, freenode).
 


Return to WIP Mods

Who is online

Users browsing this forum: No registered users and 7 guests

cron