FileNames¶
Returns a list of file names matching a pattern in the current directory.
The first argument is a string pattern, in which * stands for any
sequence of characters.
Set up a notes directory to search:
$ wo 'CreateDirectory["notes"]; CreateFile["notes/report.txt"]; CreateFile["notes/notes.txt"]; CreateFile["notes/readme.md"]; FileNames["*.txt", "notes"]'
{notes/notes.txt, notes/report.txt}
A list of patterns matches the file names matching any of them:
Patterns joined with | mean the same thing:
A third argument sets how many directory levels to include.
Set up a docs directory with a report.txt on three levels:
$ wo 'CreateDirectory["docs/inner/deeper"]; CreateFile["docs/report.txt"]; CreateFile["docs/inner/report.txt"]; CreateFile["docs/inner/deeper/report.txt"]; FileNames["*", "docs"]'
{docs/inner, docs/report.txt}
The default, 1, only looks at docs itself,
so the copies further down are not reported:
With 2, the immediate subdirectories are searched as well:
Infinity descends without a limit:
$ wo 'FileNames["report.txt", "docs", Infinity]'
{docs/inner/deeper/report.txt, docs/inner/report.txt, docs/report.txt}
A level in braces restricts the search to exactly that level:
Two levels in braces give the range of levels to search:
$ wo 'FileNames["report.txt", "docs", {2, 3}]'
{docs/inner/deeper/report.txt, docs/inner/report.txt}
Infinity works as the upper end of the range as well:
$ wo 'FileNames["report.txt", "docs", {2, Infinity}]'
{docs/inner/deeper/report.txt, docs/inner/report.txt}
The current directory is the one SetDirectory last set,
not the one the script was started from.