Difference between revisions of "Lua:getFileList"

From Cheat Engine
Jump to navigation Jump to search
(Created page with "getFileList(Path:string, searchMask:string OPTIONAL, SearchSubDirs: boolean OPTIONAL, DirAttrib: integer OPTIONAL): Returns an indexed table with filenames <pre> r = getFileLi...")
 
(Major overhaul of the post.)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
getFileList(Path:string, searchMask:string OPTIONAL, SearchSubDirs: boolean OPTIONAL, DirAttrib: integer OPTIONAL): Returns an indexed table with filenames
+
[[Category:Lua]]
<pre>
+
{{CodeBox|'''function''' getFileList(''path'', ''searchMask'' OPTIONAL, ''searchSubDirs'' OPTIONAL, ''dirAttrib'' OPTIONAL) ''':''' table}}
r = getFileList("C:\\CE\\autorun", "*.DLL", true)
+
 
require 'pl.pretty'.dump(r)
+
Returns an indexed Lua table containing filenames from the specified path.
</pre>
+
 
Print result:
+
The optional search mask can be used to limit the result to matching files. Subdirectories can optionally be searched as well.
<pre>
+
 
{
+
===Function Parameters===
  "C:\\CE\\autorun\\dlls\\MonoDataCollector32.dll",
+
{|width="85%" cellpadding="10%" cellspacing="0" border="0"
   "C:\\CE\\autorun\\dlls\\MonoDataCollector64.dll",
+
!align="left"|Parameter
   "C:\\CE\\autorun\\dlls\\32\\CEJVMTI.dll",
+
!align="left"|Type
   "C:\\CE\\autorun\\dlls\\64\\CEJVMTI.dll"
+
!style="width: 80%;background-color:white;" align="left"|Description
}
+
|-
</pre>
+
|path
 +
|String
 +
|The directory path to search in.
 +
|-
 +
|searchMask
 +
|String OPTIONAL
 +
|The filename mask to search for. For example, <code>"*.txt"</code> or <code>"*.*"</code>.
 +
|-
 +
|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====
 +
<syntaxhighlight lang="lua" line highlight="1">
 +
local files = getFileList([[C:\\Temp]])
 +
 
 +
for i, filename in ipairs(files) do
 +
  print(filename)
 +
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")
 +
 
 +
if #files == 0 then
 +
  print("No matching files found")
 +
else
 +
  for i, filename in ipairs(files) do
 +
    print(filename)
 +
  end
 +
end
 +
</syntaxhighlight>
 +
 
 +
{{LuaSeeAlso}}

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