From 172eeca7b8f2863c9bc23abf69fa87b0cd1bc760 Mon Sep 17 00:00:00 2001 From: Scott Lowe Date: Mon, 22 Feb 2016 13:30:18 +0000 Subject: [PATCH] ENH: mocov_find_files.m Add exclude patterns option Will exclude matching directories and files from the list. --- MOcov/mocov_find_files.m | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/MOcov/mocov_find_files.m b/MOcov/mocov_find_files.m index e8b9d03..36122bd 100644 --- a/MOcov/mocov_find_files.m +++ b/MOcov/mocov_find_files.m @@ -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};