Murga-Projects Forums
best way to list filenames in Lua? - Printable Version

+- Murga-Projects Forums (http://www.murga-projects.com/forum)
+-- Forum: Project Forums (/forumdisplay.php?fid=1)
+--- Forum: MurgaLua - General (/forumdisplay.php?fid=2)
+--- Thread: best way to list filenames in Lua? (/showthread.php?tid=43)


best way to list filenames in Lua? - mikshaw - 06-07-2007 10:23 AM

It just occurred to me that it looks like Lua proper doesn't seem to have any way (or any obvious way) to list filenames in a given directory.

I was attempting to grab all *.txt files in a directory using io.popen("ls "..directory) when I realized that this would work only on systems with the ls command...basically just unix-like systems. Is there any way to use Lua to get a list of text files in a directory? I'd prefer not to have to use FLTK functions, since my script is intended to eventually work in both murgaLua and Lua.

I suppose I could use the "dir" command with popen, but I'm not sure how portable that is.

edit: oh...seems the dir command doesn't work on my linux system =op

edit2: I guess even trying fltk function might not work, although that's mainly because I can't make any sense of the fl_filename_list syntax (assuming this is will actually list files):

Quote:
int fl_filename_list(const char *d, dirent ***list, Fl_File_Sort_F *sort = fl_numericsort);

This is a portable and const-correct wrapper for the scandir() function. d is the name of a directory; it does not matter if it has a trailing slash or not. For each file in that directory a "dirent" structure is created. The only portable thing about a dirent is that dirent.d_name is the nul-terminated file name. An array of pointers to these dirent's is created and a pointer to the array is returned in *list. The number of entries is given as a return value.


edit3: I guess "dir" actually does work on my system, but the results are inexplicably flawed. When i use dir in a terminal, the results are the same as using ls, but in the script the results are totally different:

Code:
for i in io.popen("ls /home/mik"):lines() do
if string.find(i,"%.txt$") then print(i) end
end

This works as expected, but when I replace "ls" with "dir" I get completely different results.
I can't count on that to work across different platforms.


RE: best way to list filenames in Lua? - mikshaw - 06-07-2007 04:43 PM

I decided, after a long google session, to just go with what I have, which is using io.popen("ls").
It is definite...there is no directory listing in Lua.
If someone happens to want to run this script on Windows or pre-OSX Mac, they'll just have to edit it.


RE: best way to list filenames in Lua? - JohnMurga - 06-07-2007 08:16 PM

mikshaw Wrote:
I decided, after a long google session, to just go with what I have, which is using io.popen("ls").
It is definite...there is no directory listing in Lua.
If someone happens to want to run this script on Windows or pre-OSX Mac, they'll just have to edit it.


What about just using :

Code:
murgaLua.readDirectory( directory );


That's what I have it in there for :-)

You can then filter the .txt files quite easily (have a look at convertFluidToMurgaLua.lua for filtering examples).

Cheers
JohnM


RE: best way to list filenames in Lua? - mikshaw - 06-08-2007 01:35 AM

Although I was hoping that the script could also work with Lua proper, this is definitely a better solution than popen. I was just pushing hard to avoid anything outside of pure Lua in this case.

However, I'm already using murgaLua.isDirectory() in the script, which I had planned to *eventually* convert to pure Lua code. The more I think about it, the more I wonder...if you wrote this function for murgaLua there must have been a reason for it, just like for murgaLua.readDirectory(). Perhaps a directory test in pure Lua is just as much work as a directory list.

I guess I'll stick with pure murgaLua =o)

Thanks.


RE: best way to list filenames in Lua? - mikshaw - 06-08-2007 09:33 AM

okie dokie...
To grab the filenames of all *.txt files in a given directory, this seems to be the most effective way:
allfiles=murgaLua.readDirectory("/home/mik")
for i=1,table.maxn(allfiles) do
if string.find(allfiles[i],"%.txt$") then print(allfiles[i]) end
end

Apparently table.getn(table) has the same result as table.maxn(table)
I dunno if one is actually better than the other.