Lua:getFileList
Jump to navigation
Jump to search
Returns an indexed Lua table containing filenames from the specified path.
The optional search mask can be used to limit the result to matching files. Subdirectories can optionally be searched as well.
Contents
Function Parameters
| Parameter | Type | Description |
|---|---|---|
| path | String | The directory path to search in. |
| searchMask | String OPTIONAL | The filename mask to search for. For example, "*.txt" or "*.*".
|
| searchSubDirs | Boolean OPTIONAL | If true, subdirectories are searched as well. |
| dirAttrib | Integer OPTIONAL | Optional directory attribute filter. |
Returns
Table — An indexed Lua table containing the matching filenames.
Examples
List all files in a directory
1 local files = getFileList([[C:\\Temp]])
2
3 for i, filename in ipairs(files) do
4 print(filename)
5 end
List files matching a search mask
1 local files = getFileList([[C:\\Temp]], "*.txt")
2
3 for i, filename in ipairs(files) do
4 print(filename)
5 end
Search subdirectories
1 local files = getFileList([[C:\\Temp]], "*.lua", true)
2
3 for i, filename in ipairs(files) do
4 print(filename)
5 end
Count matching files
1 local files = getFileList([[C:\\Temp]], "*.CT")
2
3 print("Found files: " .. tostring(#files))
Use the temporary folder
1 local tempFolder = getTempFolder()
2 local files = getFileList(tempFolder, "*.*")
3
4 for i, filename in ipairs(files) do
5 print(filename)
6 end
Filter results in Lua
1 local files = getFileList([[C:\\Temp]], "*.*")
2
3 for i, filename in ipairs(files) do
4 if filename:lower():find("%.txt$") then
5 print("Text file: " .. filename)
6 end
7 end
Build full paths from the returned filenames
1 local path = [[C:\\Temp]]
2 local files = getFileList(path, "*.lua")
3
4 for i, filename in ipairs(files) do
5 local fullPath = path .. [[\\]] .. filename
6 print(fullPath)
7 end
Guard against an empty result
1 local files = getFileList([[C:\\Temp]], "*.doesnotexist")
2
3 if #files == 0 then
4 print("No matching files found")
5 else
6 for i, filename in ipairs(files) do
7 print(filename)
8 end
9 end