Skip to content

FileNames

Returns a list of file names matching a pattern in the current directory.

$ wo 'ListQ[FileNames["*"]]'
True

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:

$ wo 'FileNames[{"*.md", "notes.txt"}, "notes"]'
{notes/notes.txt, notes/readme.md}

Patterns joined with | mean the same thing:

$ wo 'FileNames["report.txt" | "missing.txt", "notes"]'
{notes/report.txt}

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:

$ wo 'FileNames["report.txt", "docs"]'
{docs/report.txt}

With 2, the immediate subdirectories are searched as well:

$ wo 'FileNames["report.txt", "docs", 2]'
{docs/inner/report.txt, docs/report.txt}

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:

$ wo 'FileNames["report.txt", "docs", {2}]'
{docs/inner/report.txt}

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.

$ wo 'dir = CreateDirectory[]; Export[FileNameJoin[{dir, "hello.txt"}], "x"]; SetDirectory[dir]; FileNames[]'
{hello.txt}