Difference between revisions of "Lua:getFileList"

From Cheat Engine
Jump to navigation Jump to search
m
(Major overhaul of the post.)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
[[Category:Lua]]
 
[[Category:Lua]]
'''function''' getFileList(''Path'', [''SearchMask''], [''SearchSubDirs''], [''DirAttrib'']) ''':''' Table
+
{{CodeBox|'''function''' getFileList(''path'', ''searchMask'' OPTIONAL, ''searchSubDirs'' OPTIONAL, ''dirAttrib'' OPTIONAL) ''':''' table}}
  
Returns an indexed table containing the names of files in the specified path.
+
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.
  
 
===Function Parameters===
 
===Function Parameters===
Line 10: Line 12:
 
!style="width: 80%;background-color:white;" align="left"|Description
 
!style="width: 80%;background-color:white;" align="left"|Description
 
|-
 
|-
|Path
+
|path
 
|String
 
|String
|The directory path to search.
+
|The directory path to search in.
 
|-
 
|-
|SearchMask
+
|searchMask
|String (optional)
+
|String OPTIONAL
|A file mask (e.g., <code>"*.exe"</code>) to filter results. Default is <code>"*"</code>.
+
|The filename mask to search for. For example, <code>"*.txt"</code> or <code>"*.*"</code>.
 
|-
 
|-
|SearchSubDirs
+
|searchSubDirs
|Boolean (optional)
+
|Boolean OPTIONAL
|If true, searches subdirectories recursively. Default is false.
+
|If true, subdirectories are searched as well.
 
|-
 
|-
|DirAttrib
+
|dirAttrib
|Integer (optional)
+
|Integer OPTIONAL
|Directory attribute filter. Default is 0.
+
|Optional directory attribute filter.
 
|}
 
|}
  
 
===Returns===
 
===Returns===
Table — An indexed table with the names of files found.
+
Table — An indexed Lua table containing the matching filenames.
  
 
===Examples===
 
===Examples===
<pre>
+
 
local files = getFileList(getAutorunPath(), "*.txt")
+
====List all files in a directory====
for i, name in ipairs(files) do
+
<syntaxhighlight lang="lua" line highlight="1">
   print(name)
+
local files = getFileList([[C:\\Temp]])
 +
 
 +
for i, filename in ipairs(files) do
 +
   print(filename)
 
end
 
end
 +
</syntaxhighlight>
 +
 +
====List files matching a search mask====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
local files = getFileList([[C:\\Temp]], "*.txt")
 +
 +
for i, filename in ipairs(files) do
 +
  print(filename)
 +
end
 +
</syntaxhighlight>
 +
 +
====Search subdirectories====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
local files = getFileList([[C:\\Temp]], "*.lua", true)
 +
 +
for i, filename in ipairs(files) do
 +
  print(filename)
 +
end
 +
</syntaxhighlight>
 +
 +
====Count matching files====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
local files = getFileList([[C:\\Temp]], "*.CT")
 +
 +
print("Found files: " .. tostring(#files))
 +
</syntaxhighlight>
 +
 +
====Use the temporary folder====
 +
<syntaxhighlight lang="lua" line highlight="2">
 +
local tempFolder = getTempFolder()
 +
local files = getFileList(tempFolder, "*.*")
 +
 +
for i, filename in ipairs(files) do
 +
  print(filename)
 +
end
 +
</syntaxhighlight>
 +
 +
====Filter results in Lua====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
local files = getFileList([[C:\\Temp]], "*.*")
 +
 +
for i, filename in ipairs(files) do
 +
  if filename:lower():find("%.txt$") then
 +
    print("Text file: " .. filename)
 +
  end
 +
end
 +
</syntaxhighlight>
 +
 +
====Build full paths from the returned filenames====
 +
<syntaxhighlight lang="lua" line highlight="2">
 +
local path = [[C:\\Temp]]
 +
local files = getFileList(path, "*.lua")
 +
 +
for i, filename in ipairs(files) do
 +
  local fullPath = path .. [[\\]] .. filename
 +
  print(fullPath)
 +
end
 +
</syntaxhighlight>
 +
 +
====Guard against an empty result====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
local files = getFileList([[C:\\Temp]], "*.doesnotexist")
  
-- Search recursively for all .dll files
+
if #files == 0 then
local dlls = getFileList(getAutorunPath(), "*.dll", true)
+
  print("No matching files found")
for i, name in ipairs(dlls) do
+
else
  print(name)
+
  for i, filename in ipairs(files) do
 +
    print(filename)
 +
  end
 
end
 
end
</pre>
+
</syntaxhighlight>
  
== See also ==
+
{{LuaSeeAlso}}
* [[Lua:getDirectoryList|getDirectoryList]]
 

Latest revision as of 00:50, 27 June 2026

<> Lua API Reference

function getFileList(path, searchMask OPTIONAL, searchSubDirs OPTIONAL, dirAttrib OPTIONAL) : table

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.

Function Parameters[edit]

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[edit]

Table — An indexed Lua table containing the matching filenames.

Examples[edit]

List all files in a directory[edit]

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[edit]

1 local files = getFileList([[C:\\Temp]], "*.txt")
2 
3 for i, filename in ipairs(files) do
4   print(filename)
5 end

Search subdirectories[edit]

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[edit]

1 local files = getFileList([[C:\\Temp]], "*.CT")
2 
3 print("Found files: " .. tostring(#files))

Use the temporary folder[edit]

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[edit]

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[edit]

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[edit]

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

Main Pages

Core Lua documentation entry points

Lua
Script Engine