Skip to main content

Find file or directory (file.find)

Declaration

list = file.find(pattern)

Parameters

  • pattern
    Table | String.

Returns

  • list
    Array. The list of matched filenames.

Notes

Search files or directories using wildcard mode or Lua precise match, and return the matched filenames.
If pattern is a string, wildcard matching is used. Only three wildcards are supported: *, ?, [ ... ].
If pattern is a table, Lua precise matching is used.
Available in versions after 2025-04-16.

Example

Simple wildcard mode

-- Simple wildcard mode supports only three wildcards: * ? [...]
-- * matches any number of characters (including 0)
-- ? matches exactly one arbitrary character
-- [...] matches one character within the brackets, e.g. [abc]haha.txt matches ahaha.txt, bhaha.txt, chaha.txt
-- [!...] matches one character not in the brackets, e.g. [!abc]haha.txt does not match ahaha.txt/bhaha.txt/chaha.txt, but matches xhaha.txt
local results = file.find("/private/var/mobile/Containers/Shared/AppGroup/*/Library/Preferences/*.plist")

Precise table pattern mode

-- Precise Lua pattern matching using a table
-- Literal strings are treated as-is; functions that return boolean can be used to validate each segment
local results = file.find{ --<--- note the curly braces
"/private/var/mobile/Containers/Shared/AppGroup/", -- literal string, no pattern
function(s) return s:match("^[A-F0-9]+%-[A-F0-9]+%-[A-F0-9]+%-[A-F0-9]+%-[A-F0-9]+$") end, -- function to match this segment
"/Library/Preferences/",
function(s) return s:match("%.plist$") end, -- function to match this segment
}
-- You can also wrap a single string by a table to make it a pattern-matching segment
local results = file.find{ --<--- note the curly braces
"/private/var/mobile/Containers/Shared/AppGroup/", -- literal
{"^[A-F0-9]+%-[A-F0-9]+%-[A-F0-9]+%-[A-F0-9]+%-[A-F0-9]+$"}, -- pattern string wrapped in a table
"/Library/Preferences/", -- literal
{"%.plist$"}, -- pattern string wrapped in a table
}