This code is an updated version of a utility function I originally posted as an answer to the following question on Stack Overflow: "How to get all files under a specific directory in MATLAB?" (http://stackoverflow.com/q/2652630/52738).
Code repository and prior releases can be found on GitHub (https://github.com/kpeaton/dirPlus).
LIST = dirPlus(ROOTPATH) will search recursively through the folder tree beneath ROOTPATH and collect a cell array LIST of all files it finds. The list will contain the absolute paths to each file starting at ROOTPATH.
LIST = dirPlus(ROOTPATH, 'PropertyName', PropertyValue, ...) will modify how files and directories are selected, as well as the format of LIST, based on the property/value pairs specified. Valid properties that the user can set are:
'Struct' - A logical value determining if the output LIST should instead be a structure array of the form returned by the DIR function. If TRUE, LIST will be an N-by-1 structure array instead of a cell array.
'Depth' - A non-negative integer value for the maximum folder tree depth that dirPlus will search through. A value of 0 will only search in ROOTPATH, a value of 1 will search in ROOTPATH and its subfolders, etc.. Default (and maximum allowable) value is the current recursion limit set on the root object (i.e. get(0, 'RecursionLimit')).
'ReturnDirs' - A logical value determining if the output will be a list of files or subdirectories. If TRUE, LIST will be a cell array of subdirectory names/paths. Default is FALSE.
'PrependPath' - A logical value determining if the full path from ROOTPATH to the file/subdirectory is prepended to each file in LIST. The default TRUE will prepend the full path, otherwise just the file/subdirectory name is returned. This setting is ignored if the 'Struct' argument is TRUE.
'FileFilter' - A string defining a regular-expression pattern that will be applied to the file name. Only files matching the pattern will be returned in LIST. Default is '' (i.e. all files are included).
'ValidateFcn' - A handle to a function that takes as input a structure of the form returned by the DIR function and returns a logical value. This function will be applied to all files found and only files that have a TRUE return value will be included in LIST. Default is  (i.e. all files are included).
'DirFilter' - A string defining a regular-expression pattern that will be applied to the subdirectory name. Only subdirectories matching the pattern will be considered valid (i.e. included in LIST themselves or having their files included in LIST). Default is '' (i.e. all subdirectories are valid). The setting of the 'RecurseInvalid' argument determines if invalid subdirectories are still recursed down.
'ValidateDirFcn' - A handle to a function that takes as input a structure of the form returned by the DIR function and returns a logical value. This function will be applied to all subdirectories found and only subdirectories that have a TRUE return value will be considered valid (i.e. included in LIST themselves or having their files included in LIST). Default is  (i.e. all subdirectories are valid). The setting of the 'RecurseInvalid' argument determines if invalid subdirectories are still recursed down.
'RecurseInvalid' - A logical value determining if invalid subdirectories (as identified by the 'DirFilter' and 'ValidateDirFcn' arguments) should still be recursed down. Default is FALSE (i.e the recursive searching stops at invalid subdirectories).
Examples for how to use these options can be found in the demo script getAllFiles_demo.m or in published form in getAllFiles_demo.html.
Kenneth Eaton (2020). dirPlus (https://www.mathworks.com/matlabcentral/fileexchange/60716-dirplus), MATLAB Central File Exchange. Retrieved .
Is the output guaranteed to be sorted?
If you can provide an example to show how to select files with specific names, like "SAT*.m' (some m file names starting with SAT), it would be much more helpful.
very useful ode.
many thanks indeed !
nevermind - I had overlooked the 'Depth', 0 parameter for just searching the rootFolder.
it would be nice if there was a clear option on how to exclude ALL subdirectories. I can't think of a proper filter atm that could programatically exclude arbitrarily named subdirectories.
I had written this with a focus on only collecting file lists, but you've given me an idea to extend it to let users collect lists of subdirectories as well. For now, there's no *complete* way to do what you want. If you collect all files, extract the paths with the `fileparts` function, and then use the function `unique` on the results, it will give you a list of all the subdirectories that have at least one file in them. It won't include empty subdirectories though.
Thanks for the feedback! I hope to have a new version posted in the next couple days, probably with a new name like `dirPlus` or similar.
Ken, this is great!! Is there a way to get a list of subdirectories without using genpath? I tried to amend your code to spit out the list of subdirectories but I am unfamiliar with the way you have implemented the parser.
The 'FileFilter' argument is a regular expression pattern, so you can look for multiple matches using just that one string. For example, you can find all .jpg, .png, and .bmp files like so: '\.(jpg|png|bmp)$'.
Can I Specify more than 1 only 'FileFilter' ?
Inspired: EnergyPlus Co-simulation Toolbox
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!