Permalink
Browse files

ENH: mocov_find_files.m Add exclude patterns option

Will exclude matching directories and files from the list.
  • Loading branch information...
1 parent 924fd09 commit 172eeca7b8f2863c9bc23abf69fa87b0cd1bc760 @scottclowe scottclowe committed Feb 22, 2016
Showing with 24 additions and 3 deletions.
  1. +24 −3 MOcov/mocov_find_files.m
View
@@ -1,4 +1,4 @@
-function res=mocov_find_files(root_dir, file_pat, monitor)
+function res=mocov_find_files(root_dir, file_pat, monitor, exclude_pat)
% Finds files recursively in a directory
%
% res=mocov_find_files([root_dir[, file_pat]])
@@ -12,6 +12,7 @@
% is used, corresponding to all files.
% monitor Optional progress monitory that supports a
% 'notify' method.
+% exclude_pat Optional cell array of patterns to exclude.
%
% Output:
% res Kx1 cell with names of files in root_dir matching
@@ -35,10 +36,28 @@
monitor=[];
end
+ if nargin<4
+ exclude_pat={};
+ end
+
+ if ischar(exclude_pat)
+ exclude_pat={exclude_pat};
+ end
+
file_re=['^' ... % start of the string
regexptranslate('wildcard',file_pat) ...
'$']; % end of the string
+ exclude_re='';
+ for k=1:numel(exclude_pat)
+ if ~isempty(exclude_re)
+ exclude_re=[exclude_re '|'];
+ end
+ exclude_re=[exclude_re ...
+ '^' ... % start of the string
+ regexptranslate('wildcard',exclude_pat{k}) ...
+ '$']; % end of the string
+ end
if ~isempty(monitor)
msg=sprintf('Finding files matching %s from %s',file_pat,root_dir);
@@ -47,7 +66,7 @@
res=find_files_recursively(root_dir,file_re,monitor);
-function res=find_files_recursively(root_dir,file_re,monitor)
+function res=find_files_recursively(root_dir,file_re,monitor,exclude_re)
if isempty(root_dir)
dir_arg={};
else
@@ -67,7 +86,9 @@
if ~ignore_fn
path_fn=fullfile(root_dir, fn);
- if isdir(path_fn)
+ if ~isempty(regexp(fn,exclude_re,'once'));
+ continue;
+ elseif isdir(path_fn)
res=find_files_recursively(path_fn,file_re,monitor);
elseif ~isempty(regexp(fn,file_re,'once'));
res={path_fn};

0 comments on commit 172eeca

Please sign in to comment.