[texhax] list files with lua: windows vs linux

Reinhard Kotucha reinhard.kotucha at web.de
Sat Apr 9 01:09:14 CEST 2016

On 2016-04-08 at 22:22:34 +0200, Arno Trautmann wrote:

 > Hi all,
 > thanks for the suggestion. I already got a solution by Ulrich in a
 > (by accident?) private mail following your ansatz of sorting. For
 > the archives, I post the now working code snippet below.

Hi Arno,
just for the record, on reasonable operating systems directories are
never sorted.  It would simply take too much time.  Sorting makes
only sense if you create a listing supposed to be read by a human.
Furthermore, if you create such a listing, the order depends on the
current locale, LC_COLLATE in this case.  Thus, only commands like
ls(1) sorts filenames.

Since lfs.dir() depends on the O/S function readdir(), you always have
to sort the results afterwards yourself.  This is unrelated to Lua or
lfs.dir, Perl's readdir() function also returns an unordered list.

If Windows returns a sorted list at such a low level, its lethargy is
explainable.  Today I've got a new machine at work and saved the
content of the old disk to a USB drive.  Copying the content of the
USB drive to the new disk took one hour, wiping out the USB drive took
half an hour.  Deleting files is usually a cheap operation, especially
because the contents of files is not deleted.  Does Windows really
sort directories alphabetically before removing them?

 > if string.find(file,".eps") then

Doesn't Lua treat the second argument of string.find() as a Lua
regexp?  If so, the dot has to be quoted:

   if string.find(file,"%.eps") then

BTW, Lua's table.sort() sorts strings alphabetically by default, hence


should be sufficient.  What is the purpose of

  return (type(a) == type(b)) and (a < b) or (type(a) < type(b))

? Does lfs.dir() return anything but strings (file names)?


Reinhard Kotucha                            Phone: +49-511-3373112
Marschnerstr. 25
D-30167 Hannover                    mailto:reinhard.kotucha at web.de

More information about the texhax mailing list