1function fileCell = findfiles(varargin) 2%FINDFILES Recursively search directory for files. 3% FINDFILES returns a cell array with the file names of all files 4% in the current directory and all subdirectories. 5% 6% FINDFILES(DIRNAME) returns the file names of all files in the given 7% directory and its subdirectories. 8% 9% FINDFILES(DIRNAME, FILESPEC) only returns the file names matching the 10% given file specification (like '*.c' or '*.m'). 11% 12% FINDFILES(DIRNAME, FILESPEC, 'nonrecursive') searches only in the top 13% directory. 14% 15% FINDFILES(DIRNAME, FILESPEC, EXLUDEDIR1, ...) excludes the additional 16% directories from the search. 17% 18% Markus Buehren 19% Last modified 13.11.2007 20% 21% See also DIR. 22 23if nargin == 0 24 searchPath = '.'; 25 fileSpec = '*'; 26elseif nargin == 1 27 searchPath = varargin{1}; 28 fileSpec = '*'; 29else 30 searchPath = varargin{1}; 31 fileSpec = varargin{2}; 32end 33 34excludeCell = {}; 35% searchrecursive = true; 36for n=3:nargin 37 if isequal(varargin{n}, 'nonrecursive') 38 % searchrecursive = false; 39 elseif iscell(varargin{n}) 40 excludeCell = [excludeCell, varargin{n}]; %#ok 41 elseif ischar(varargin{n}) && isdir(varargin{n}) 42 excludeCell(n+1) = varargin(n); 43 else 44 error('Directory not existing or unknown command: %s', varargin{n}); 45 end 46end 47 48searchPath = chompsep(searchPath); 49if strcmp(searchPath, '.') 50 searchPath = ''; 51elseif ~exist(searchPath, 'dir') 52 error('Directory %s not existing.', searchPath); 53end 54 55% initialize cell 56fileCell = {}; 57 58% search for files in current directory 59dirStruct = dir(concatpath(searchPath, fileSpec)); 60for n=1:length(dirStruct) 61 if ~dirStruct(n).isdir 62 fileCell(end+1) = {concatpath(searchPath, dirStruct(n).name)}; %#ok 63 end 64end 65 66% % search in subdirectories 67% if searchrecursive 68% excludeCell = [excludeCell, {'.', '..'}]; 69% if isempty(searchPath) 70% dirStruct = dir('.'); 71% else 72% dirStruct = dir(searchPath); 73% end 74% 75% for n=1:length(dirStruct) 76% if dirStruct(n).isdir 77% name = dirStruct(n).name; 78% if ~any(strcmp(name, excludeCell)) 79% fileCell = [fileCell, findfiles(concatpath(searchPath, name), fileSpec)]; %#ok 80% end 81% end 82% end 83% end 84 85%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 86function str = chompsep(str) 87%CHOMPSEP Remove file separator at end of string. 88% STR = CHOMPSEP(STR) returns the string STR with the file separator at 89% the end of the string removed (if existing). Further, all file 90% separators are replaced by the system-dependent file separator ("\" on 91% Windows, "/" on other systems). 92 93fsep = filesep; 94if fsep == '\' 95 str = strrep(str, '/', '\'); 96else 97 str = strrep(str, '\', '/'); 98end 99 100if ~isempty(str) && str(end) == fsep 101 str(end) = ''; 102end 103 104%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 105function str = concatpath(varargin) 106%CONCATPATH Concatenate file parts with correct file separator. 107% STR = CONCATPATH(STR1, STR2, ...) concatenates file/path parts with the 108% correct file separator. 109 110str = ''; 111for n=1:nargin 112 curStr = varargin{n}; 113 if n==1 114 str = chompsep(curStr); 115 else 116 str = fullfile(str, chompsep(curStr)); 117 end 118end 119 120fileSep = filesep; 121if fileSep == '\' 122 str = strrep(str, '/', '\'); 123else 124 str = strrep(str, '\', '/'); 125end 126 127