-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_recursive_filenames.m
71 lines (57 loc) · 1.54 KB
/
get_recursive_filenames.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
% GET_RECURSIVE_FILENAMES get filenames matching the filefilter
% in a directory and in all subdirectories
%
% $Id: get_recursive_filenames.m 841 2010-08-11 03:38:32Z joey $
%
% basedir - base directory to search
% ffilter - (optional) string or wildcard eg 'foo.mat' or '*.plx' or a regexp
% is_regexp - (optioinal) indicates if ffilter is a regexp (default: false)
function [paths] = get_recursive_filenames(basedir, varargin)
error(nargchk(1,3,nargin,'struct'));
ffilter = '';
is_regexp = 0;
for i=2:nargin
if (i==2)
ffilter = varargin{i-1};
elseif (i==3)
is_regexp = varargin{i-1};
end
end
if strcmp(ffilter,'')
if is_regexp
ffilter = '.';
else
ffilter = '*';
end
end
% start at the top
d = dir(basedir);
% make the ffilter regexp safe
if ~is_regexp
tmp_filter = regexptranslate('wildcard',ffilter);
else
tmp_filter = ffilter;
end
% match whole word that contains the pattern
tmp_filter = strcat('\S*',tmp_filter);
% apply regexp
f = regexp({d.name},tmp_filter,'match','once');
% remove empty cells
f = f(~cellfun(@isempty,f));
if ~isempty(f)
f = f(:);
% pad with basedir
tmpdir = cellstr(repmat(basedir,length(f),1));
f = cellfun(@fullfile,tmpdir,f,'UniformOutput',false);
else
f = {};
end
% get all directories in basedir
dirs = {d([d.isdir]).name};
dirs = dirs(3:end)'; % excluding '.' and '..'
% recurse!
for j=1:length(dirs)
f_tmp = get_recursive_filenames(fullfile(basedir,dirs{j}),ffilter,is_regexp);
f = [f ; f_tmp];
end
paths = f;