-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindfiles.m
More file actions
91 lines (79 loc) · 2.88 KB
/
findfiles.m
File metadata and controls
91 lines (79 loc) · 2.88 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
function fnames = findfiles(extension,directory,recurse)
% Finds all files with the specified extension in the current directory and
% subdirectories (recursive). Returns a cell array with the fully specified
% file names.
%
% files = findfiles( ext )
% searches in the current directory and subdirectories.
%
% files = findfiles( ext, directory)
% starts search in directory specified.
%
% files = findfiles( ext, directory, searchSubdirectories )
% If searchSubdirectories == 0, the function will not descend
% subdirectories. The default is that searchSubdirectories ~= 0.
%
% Note: if this function causes your recursion limit to be exceeded, then
% it is most probably trying to follow a symbolic link to a directory that
% has a symbolic link back to the directory it came from.
%
% Examples:
%
% bmpfiles = findfiles('bmp')
% dllfiles = findfiles('dll','C:\WinNT')
% mfiles = findfiles('m',matlabroot)
% Copyright (C) 2001 Quester Tangent Corporation
% Author: Tony Christney, tchristney@questertangent.com
% $Id: findfiles.m 1.5 2001/05/25 01:54:17 tchristney Exp $
% $Log: findfiles.m $
% Revision 1.5 2001/05/25 01:54:17 tchristney
% Fixed bug where numMatches was not updated after recursive call, causing
% potential overwrites in the fnames cell array.
%
% Revision 1.4 2001/05/24 20:46:09 tchristney
% Recursion on/off support courtesy Brett Shoelson.
% <Brett.Shoelson@joslin.harvard.edu>
%
% Revision 1.2 2001/03/16 21:28:37 tchristney
% added some comments for external release, i.e. examples.
%
% Revision 1.1 2001/03/07 22:38:52 tchristney
% Initial revision
%
if nargin == 1
directory = cd;
recurse = (1==1);
elseif nargin == 2
oldDir = cd;
cd(directory);
directory = cd;
cd(oldDir);
recurse = (1==1);
elseif nargin >= 3
oldDir = cd;
cd(directory);
directory = cd;
cd(oldDir);
end
d = dir(directory);
fnames = {};
numMatches = 0;
for i=1:length(d)
% look for occurences of ['.' extension] in the file name
extIndices = findstr(['.' extension],d(i).name);
% if the file is not a directory, and the file has at least one occurence
if ~d(i).isdir & ~isempty(extIndices)
% then if the last occurrence is at the end of the file name,
% add the file name to the list.
if length(d(i).name) == (extIndices(length(extIndices)) + length(extension))
numMatches = numMatches + 1;
fnames{numMatches} = fullfile(directory,d(i).name);
end
% otherwise, descend directories appropriately.
% note that this could result in a recursion limit error if it tries to
% follow symbolic links that loop back on themselves...
elseif recurse & d(i).isdir & ~strcmp(d(i).name,'.') & ~strcmp(d(i).name,'..')
fnames = [fnames findfiles(extension,fullfile(directory,d(i).name),recurse)];
numMatches = length(fnames);
end
end