-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfind_files.m
48 lines (47 loc) · 1.42 KB
/
find_files.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
function files = find_files(varargin)
% FIND_FILES Perform a recursive directory search
%
% files = find_files('data') Find all the files in the data directory.
% files = find_files('data','.txt.gz') Find all the files in the
% data directory with extension *.txt.gz
%
if length(varargin) < 1
curdir = '.';
else
curdir = varargin{1};
if length(varargin) > 1
exts = varargin(2:end);
else
exts = {}; % all extensions
end
end
% we are searching curdir recursively for files with an extension matching
% that in the list of extensions
filesstruct = dir(curdir);
files = {};
mydirfiles = {};
for fi=1:length(filesstruct)
f = filesstruct(fi);
if strcmp(f.name,'.') || strcmp(f.name,'..')
continue
elseif f.isdir
% recurse
dirfiles = find_files(fullfile(curdir,f.name),exts{:});
files = [files; dirfiles]; % append the list of files
else
% it's a regular file, see if it matches the extension list
% TODO escape periods in the name list
if isempty(exts)
mydirfiles{end+1} = fullfile(curdir,f.name);
else
for exti=1:length(exts)
if ~isempty(regexp(f.name,[exts{exti} '$'], 'once'))
% it's a match!
mydirfiles{end+1} = fullfile(curdir,f.name);
break;
end
end
end
end
end
files = [files;mydirfiles'];