function dirlist = regexpdir(rootdir, expstr, recursive)
% REGEXPDIR Gives a directory listing based on a regular expression
% REGEXPDIR(ROOTDIR, REGEXP) gives a directory listing of the directory
% ROOTDIR based on the pattern specified by the regular expression
% REGEXP.
%
% REGEXPDIR(ROOTDIR, REGEXP, RECURSIVE) By default REGEXPDIR traverses
% all subdirectories recursively. This behaviour can be controlled by
% supplying the optional boolean RECURSIVE. Setting this to 'false' will
% limit the function to the directory specified in ROOTDIR.
%
% Example:
% rootdir = 'C:\';
% expstr = '^.*\.exe$';
% dirlist = regexpdir(rootdir, expstr);
%
% The above example will return any EXE files on the C-drive.
%
% By default REGEXPDIR searches case insensitive. To make it case
% senstitive please specify it in the regular expression by adding
% '(?-i)' to it. Ommitting to specify the beginning '^' and ending '$'
% of the regular expression may result in unexpected behaviour.
%
% Have a look at the source code for more information. For more
% info on this function and how to use it, bug reports, feature
% requests, etc. feel free to contact the author.
%
% See also DIR, REGEXP, REGEXPTRANSLATE
%==========================================================================
% B.C. Hamans (b.c.hamans@rad.umcn.nl) 2007
%==========================================================================
% Check input arguments
error(nargchk(2, 3, nargin));
if ~exist('recursive','var'); recursive = true; end
% Check if the root directory contains a trailing file seperator or supply
rootdir = char(rootdir);
if ~strcmp(rootdir(length(rootdir)), filesep); rootdir=[rootdir filesep]; end
% Remember initial starting directory when traversing.
persistent basedir; if isempty(basedir); basedir = rootdir; end
% Traverse the structure
dirlist = cell({}); dirtree = dir(rootdir);
for h = find([dirtree.isdir]);
if ~any(strcmp({'.','..'}, dirtree(h).name));
if regexpi(strrep(fullfile(rootdir, dirtree(h).name, filesep), basedir, ''), expstr);
dirlist = [dirlist; fullfile(rootdir, dirtree(h).name, filesep)];
end
if recursive;
dirlist = [dirlist; regexpdir(fullfile(rootdir, dirtree(h).name, filesep), expstr, recursive)];
end
end
end
for i = find(~[dirtree.isdir]);
if regexpi(strrep(fullfile(rootdir, dirtree(i).name), basedir, ''), expstr);
dirlist = [dirlist; fullfile(rootdir, dirtree(i).name)];
end
end
%==========================================================================
% Changelog:
% 03-09-2007 v1.00 (BCH) Initial release
% 20-09-2007 v1.01 (BCH) Proper solution for persistent variable 'basedir'
%==========================================================================