List files in a directory (file.list)
Declaration
list, err = file.list(path [, deep])
Parameters
- path
String. Absolute directory path. - deep
Boolean, optional. Added in 2025-03-13. Whether to recursively return full paths of files in subdirectories. Default false.
Returns
- list
Array or nil. nil if path does not exist or path is a file; otherwise an array of file names in the directory. - err
String. Error message when operation fails.
Notes
Get all file names under a directory.
Example
local list, err = file.list("/var/mobile/")
if list then
print("There are "..#list.." files or directories in /var/mobile/")
for _, name in ipairs(list) do
print(name)
end
sys.alert(print.out())
else
sys.alert("/var/mobile/ is not a directory: "..err)
end
Note: Uses sys.alert
Deep recursive example
-- full_paths = file.list(path, deep)
-- When deep is true, returns full paths recursively
list = file.list("/var/mobile/Media/1ferver", true)
nLog(list)
--[[
Possible output
{ -- table: 0xc4cc58a30
[1] = "/var/mobile/Media/1ferver/snippets/syntax - do __ end.snippet",
[2] = "/var/mobile/Media/1ferver/snippets/app - app.uninstall(bid).snippet",
[3] = "/var/mobile/Media/1ferver/snippets/test - snippet.snippet",
...
}
--]]
Note: Uses nLog