list_files
result = list_files([dir], [pattern], [filter=], [contains=], [size=], [recursive=])
Input: dir, pattern, filter, contains, size, recursive · Output: result
What it is
Every file matching a whole-path wildcard, as full paths, sorted — never a directory, which is what makes the content and size filters mean something on every entry. dir is a directory to search and pattern a wildcard to match (* and ? stay inside one path segment, ** crosses them); a single argument is read as a pattern when it holds a wildcard and as a directory when it does not. filter= is the keyword spelling of the pattern; contains= keeps only files whose bytes hold that text; size= compares against the byte size (">2MB", "<=100KB", "=0", with 1024-based units); recursive= searches subdirectories without writing ** yourself. glob is the same search with no filters, under the name most other languages give it.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = list_files([dir], [pattern], [filter=], [contains=], [size=], [recursive=]) | dir optionaldir is a directory to search and pattern a wildcard to match (* and ? stay inside one path segment, ** crosses them).pattern optionala single argument is read as a pattern when it holds a wildcard and as a directory when it does not.filter optionalfilter= is the keyword spelling of the pattern.contains optionalcontains= keeps only files whose bytes hold that text.size optionalsize= compares against the byte size (">2MB", "<=100KB", "=0", with 1024-based units).recursive optionalrecursive= searches subdirectories without writing ** yourself. | result List or Str | Returns a List of Str, empty when nothing matched. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
mkdir("list_demo")
write_text("list_demo/report.qu", "print(1)")
write_text("list_demo/plot.qu", "print(2)")
write_text("list_demo/notes.txt", "the needle is here")
make_file("list_demo/empty.log")
qu_files = list_files("list_demo", "*.qu")
print("{length(qu_files)} Qu files")
carrying = list_files("list_demo/*", contains = "needle")
for f in carrying
print("carries the needle: {f}")
end for
blank = list_files("list_demo/*", size = "0")
print("{length(blank)} empty file")
2 Qu files carries the needle: list_demo\notes.txt 1 empty file
from file-io
See also
glob — documented in the same entry.