News : The level of daily SPAM has reached insane proportions, all registrations are now manual. I ask you to send me an e-mail (john (at) murga (dot) org), to confirm that you want me to create an account for you.

Post Reply  Post Thread 
best way to list filenames in Lua?
Author Message
Senior Member

Posts: 522
Group: Registered
Joined: Apr 2007
Status: Offline
Reputation: 5
Post: #1
best way to list filenames in Lua?

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 " 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):

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:

for i in io.popen("ls /home/mik"):lines() do
if string.find(i,"%.txt$") then print(i) 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.

This post was last modified: 06-07-2007 12:36 PM by mikshaw.

06-07-2007 10:23 AM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

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

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites

Forum Jump: