Recursively list files and folders

Recursively lists files and folders under the specified root path using options to filter results
42 Downloads
Updated 3 Jul 2024

View License

Summary
This function aims to simplify the process of recursively listing the contents of a folder tree by the use of optional name-value pairs that provide fine grained control over the items to be returned.
The main difference between this function and the many alternatives that exist on FEX is a simple and intuitive set of options to filter the search results.
Examples
1) List all files and folders under the current directory
combinedListing = recursiveDir(pwd());
2) List all MATLAB (.m) files under the current directory
combinedListing = recursiveDir(pwd(), "FileType", ".m");
3) List all folders under the current directory with depth limit of 2
combinedListing = recursiveDir(pwd(), "Type", "folder", "RecursionDepth", 2);
4) List all files and folders under multiple root paths
rootPaths = ["path1", "path2", "path3"];
combinedListing = recursiveDir(rootPaths);
Options
The following options are available for customizing the result of listing the contents of a folder tree:
IgnoreList - List of words/substrings to ignore during
listing. Default is an empty string array.
Expression - Regular expression to filter files and folders.
Default is an empty string.
Type - Type of items to list: 'file', 'folder', or
'any'. Default is 'any'.
FileType - File extension for filtering specific files.
Default is an empty string.
RecursionDepth - Maximum recursion depth. Default is inf.
IsCumulative - Flag to indicate whether to accumulate files
and/or folders as the function goes deeper into
the directory tree. Default is true. If false,
only files and folders from the depth specified
in RecursionDepth will be returned.
OutputType - Type of output: 'FileAttributes', 'FilePath' or
'RelativeFilePath'. Default is 'FileAttributes'
(struct array as returned by the builtin dir
function).
IncludeHiddenFiles - Flag to include hidden files and folders.
Default is false (Unix only).

Cite As

Eivind Hennestad (2026). Recursively list files and folders (https://www.mathworks.com/matlabcentral/fileexchange/160058-recursively-list-files-and-folders), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2023b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags
Version Published Release Notes
1.0.3

Fix: There was a bug leading to comparison of logical vectors of different lengths if value of Expression was non-empty and items matched patterns in IgnoreList

1.0.2

Added "RelativeFilePath" as an alternative for "OutputType

1.0.1

Update description

1.0.0