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