Wolfram: List Files (Walk Directory)

By Xah Lee. Date: . Last updated: .

Get Dir Content (List File Names)

summary

FileNames[strPattern, dir, depth]

return a List of all files and directories in directory dir up to depth.

  • The pattern strPattern can be a wildcard pattern or Wolfram: Regular Expression or Wolfram: String Expression
  • strPattern must match the file fullpath completely, not just containing the pattern.
  • strPattern can be a list of patterns.
  • strPattern can be All, means all files.
  • dir can be a fullpath or relative path, or "." for current dir.
  • dir can be a list of dirs.
  • depth is a integer or Infinity. It means depth of dir.
(* list all files in a dir, all subdirs  *)
FileNames[All, "c:/Users/xah/web/xahlee_info/M", Infinity]

(* 
{c:/Users/xah/web/xahlee_info/M\detect_zero_vector.html,
c:/Users/xah/web/xahlee_info/M\Elementary_Intro_to_Wolfram_Language_Review.html,
c:/Users/xah/web/xahlee_info/M\history_of_notebook.html,
c:/Users/xah/web/xahlee_info/M\i,
c:/Users/xah/web/xahlee_info/M\ic,
c:/Users/xah/web/xahlee_info/M\ic\plane_curve_2024-03-01_132521.png,
c:/Users/xah/web/xahlee_info/M\ic\plane_curve_2024-03-01_132538.png
}
*)

Current Directory (Only File Names vs Full Path vs Relative Path)

(* if no dir is given, use current dir. and return values are just file names. *)
FileNames["*.txt"]

(*
{xah-find-2025-07-09_075353_5fa.txt,
xah-find-2025-07-09_094715_516.txt,
xah-find-2025-07-10_085007_5f0.txt,
xnew.txt,
xx_2025-04-13_104411_eac55.txt,
xx_2025-05-27_151301_77fc2.txt,
xx_2025-07-08_192442_81428.txt}
*)

(* HHHH------------------------------ *)

(* if dir is given, and is full path, return values are full path. *)
FileNames["*.txt", "c:/Users/xah/.emacs.d/temp"]

(*
{c:/Users/xah/.emacs.d/temp\xah-find-2025-07-09_075353_5fa.txt,
c:/Users/xah/.emacs.d/temp\xah-find-2025-07-09_094715_516.txt,
c:/Users/xah/.emacs.d/temp\xah-find-2025-07-10_085007_5f0.txt,
c:/Users/xah/.emacs.d/temp\xnew.txt,
c:/Users/xah/.emacs.d/temp\xx_2025-04-13_104411_eac55.txt,
c:/Users/xah/.emacs.d/temp\xx_2025-05-27_151301_77fc2.txt,
c:/Users/xah/.emacs.d/temp\xx_2025-07-08_192442_81428.txt}
*)

(* HHHH------------------------------ *)

(* if dir is given, and is relative path, return values are relative path. *)
FileNames["*.txt", "../temp"]

(*
{../temp\xah-find-2025-07-09_075353_5fa.txt,
../temp\xah-find-2025-07-09_094715_516.txt,
../temp\xah-find-2025-07-10_085007_5f0.txt,
../temp\xnew.txt,
../temp\xx_2025-04-13_104411_eac55.txt,
../temp\xx_2025-05-27_151301_77fc2.txt,
../temp\xx_2025-07-08_192442_81428.txt}
 *)

List Files of Dir and Subdirs (recurse)

List all files, including any subdirectories to infinite depth:

FileNames[ All, "c:/Users/xah/Downloads/" , Infinity]

up to depth 2:

FileNames[ All, "c:/Users/xah/Downloads/" , 2]

List Files, Filter by File Extension

FileNames["*.html","c:/Users/xah/web/"]
FileNames[
{"*.html", "*.css"},
 "c:/Users/xah/"]

List Files of Multiple Directories

list all file names ending in .html or .css, in several directories:

FileNames[
{"*.html", "*.css"},
{"c:/Users/xah/Documents/",
 "c:/Users/xah/Downloads/"}]

Map Function to Files

Wolfram. Shell Scripting Tasks