NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/gliderproc/trunk/MATLAB/util/findfiles.m

Revision 495 (checked in by cbc, 11 years ago)

Initial import of Stark code.

Line 
1 function fnames = findfiles(extension,directory)
2 % Finds all files with the specified extension in the current directory and
3 % subdirectories (recursive). Returns a cell array with the fully specified
4 % file names.
5 %
6 %    files = findfiles( ext )
7 %       searches in the current directory and subdirectories.
8 %
9 %    files = findfiles( ext, directory)
10 %       starts search in directory specified.
11 %
12 % Note: if this function causes your recursion limit to be exceeded, then
13 % it is most probably trying to follow a symbolic link to a directory that
14 % has a symbolic link back to the directory it came from.
15 %
16 % Examples:
17 %
18 %    bmpfiles = findfiles('bmp')
19 %    dllfiles = findfiles('dll','C:\WinNT')
20 %    mfiles = findfiles('m',matlabroot)
21
22 % Copyright (C) 2001 Quester Tangent Corporation
23 % Author: Tony Christney, tchristney@questertangent.com
24 % $Id: findfiles.m 1.2 2001/03/16 21:28:37 tchristney Exp $
25
26 % $Log: findfiles.m $
27 % Revision 1.2  2001/03/16 21:28:37  tchristney
28 % added some comments for external release, i.e. examples.
29 %
30 % Revision 1.1  2001/03/07 22:38:52  tchristney
31 % Initial revision
32 %
33
34 if nargin == 1
35     directory = cd;
36 elseif nargin >= 2
37     oldDir = cd;
38     cd(directory);
39     directory = cd;
40     cd(oldDir);
41 end
42
43 d = dir(directory);
44
45 fnames = {};
46 numMatches = 0;
47 for i=1:length(d)
48     % look for occurences of ['.' extension] in the file name
49     extIndices = findstr(['.' extension],d(i).name);
50    
51     % if the file is not a directory, and the file has at least one occurence
52     if ~d(i).isdir & ~isempty(extIndices)
53        
54         % then if the last occurrence is at the end of the file name,
55         % add the file name to the list.
56         if length(d(i).name) == (extIndices(length(extIndices)) + length(extension))
57             numMatches = numMatches + 1;
58             fnames{numMatches} = fullfile(directory,d(i).name);
59         end
60     % otherwise, descend directories appropriately.
61     % note that this could result in a recursion limit error if it tries to
62     % follow symbolic links that loop back on themselves...
63     elseif d(i).isdir & ~strcmp(d(i).name,'.') & ~strcmp(d(i).name,'..')
64         fnames = [fnames findfiles(extension,fullfile(directory,d(i).name))];
65     end
66 end
Note: See TracBrowser for help on using the browser.