dirPlus
dirPlus
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?" Although written in MATLAB version R2016b, I believe this should work for older versions as well (full testing still required).
dirPlus
will recursively collect files/subdirectories within a folder that match a certain set of criteria. 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:
GENERAL:
-
'Struct'
- A logical value determining if the outputLIST
should instead be a structure array of the form returned by thedir
function. IfTRUE
,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 thatdirPlus
will search through. A value of 0 will only search inROOTPATH
, a value of 1 will search inROOTPATH
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. IfTRUE
,LIST
will be a cell array of subdirectory names/paths. Default isFALSE
. -
'PrependPath'
- A logical value determining if the full path fromROOTPATH
to the file/subdirectory is prepended to each file inLIST
. The defaultTRUE
will prepend the full path, otherwise just the file/subdirectory name is returned. This setting is ignored if the'Struct'
argument isTRUE
.
FILE-SPECIFIC:
-
'FileFilter'
- A string defining a regular-expression pattern that will be applied to the file name. Only files matching the pattern will be included inLIST
. 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 thedir
function and returns a logical value. This function will be applied to all files found and only files that have aTRUE
return value will be included inLIST
. Default is[]
(i.e. all files are included).
DIRECTORY-SPECIFIC:
-
'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 inLIST
themselves or having their files included inLIST
). 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 thedir
function and returns a logical value. This function will be applied to all subdirectories found and only subdirectories that have aTRUE
return value will be considered valid (i.e. included inLIST
themselves or having their files included inLIST
). 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 isFALSE
(i.e the recursive searching stops at invalid subdirectories).
Examples for how to use these options can be found in the demo script dirPlus_demo.m or in published form in dirPlus_demo.html.
Note: The code in the master branch may not be fully tested or stable. Stable, tested releases appear in the Releases tab. Additional information can be found on the MathWorks File Exchange submission page.
Zitieren als
Kenneth Eaton (2024). dirPlus (https://www.mathworks.com/matlabcentral/fileexchange/60716-dirplus), MATLAB Central File Exchange. Abgerufen.
Kompatibilität der MATLAB-Version
Plattform-Kompatibilität
Windows macOS LinuxKategorien
Tags
Quellenangaben
Inspiriert: EnergyPlus Co-simulation Toolbox
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Live Editor erkunden
Erstellen Sie Skripte mit Code, Ausgabe und formatiertem Text in einem einzigen ausführbaren Dokument.
html/
Version | Veröffentlicht | Versionshinweise | |
---|---|---|---|
2.0.0.0 | Changed name from getAllFiles to dirPlus, added 'ReturnDirs' option to collect subdirectory lists, added 'DirFilter' and 'ValidateDirFcn' options to select valid directories, and added 'RecurseInvalid' option to modify recursive behavior. |
||
1.4.0.0 | Added a new 'Struct' option for returning as output a structure array of file information instead of just a cell array of file names/paths.
|
||
1.3.0.0 | Updated help and demo. |
||
1.2.0.0 | Added 'Depth' option. |
||
1.1.0.0 | Modified the data that's passed to the validation function. It should now accept as input a structure of the form returned by the DIR function instead of just a file name. |
||
1.0.0.0 | Bad description formatting. |