The bad news: Lua does not actually know about file systems. Lua is designed within the limits of what can be done in ANSI C, and that 'platform' doesn't know about directories. If this seems bizarre, bear in mind that Lua can and does run on embedded platforms that don't even have a file system.
You can use io.popen
and directly use the shell to tell you what the contents of a directory is. But then you will have to keep cross-platform differences in mind (use 'ls' or 'dir /b'?) and so this gets messy. But it can be done - see LuaRocks as an example of a sophisticated Lua program which only leverages the standard libraries and the underlying shell for its operation.
A cleaner solution is to use an extension library like LuaFileSystem:
> require 'lfs'
> for f in lfs.dir '.' do print(f) end
.
..
lua lrun lual lua51
scite lscite lrocks luagdb luajit
> a = lfs.attributes 'lua'
> for k,v in pairs(a) do print(k,v) end dev 64772
change 1248283403
access 1248354582
rdev 0
nlink 1
blksize 4096
uid 1003
blocks 320
gid 100
ino 7148
mode file modification 1245230803
size 163092
Which tells me that 'lua' is a file, and has size of 163092 bytes. The time fields like 'modification' and 'access' can be translated into sensible dates using os.date
.
If you prefer a higher-level library that harnesses LuaFileSystem, then look at pl.dir which provides a higher-level way to work with directories and provides convenience functions to copy or move files.