function file = findFiles(src,str,maxdepth) % function file = findFiles(src,str,maxdepth) % % returns a cell array of full pathnames to files in directory string "src" % with optional expression "str". Defaults to all non-hidden files in % specified directory. Directory recursion can be enabled with maxdepth integer. % % Example: (WERA yearDay directories) % src '/home/sunset1/data/sort/kna/' % str '???????/*.SORT' % T.Hilmer, UH % 2011.01.13 version 2 % added recursion modifier file = []; % default empty if nargin < 2 str = '*'; % everything end if nargin < 3 % recursion is default method of find, but non-recursion is default of % this function maxdepth = 1; else assert(isnumeric(maxdepth),'"maxdepth" must be an integer') maxdepth = round(maxdepth); end maxdepth = sprintf(' -maxdepth %.0f',maxdepth); if ~strcmp(src(end),filesep) % ensure directory/ final slash src = [src filesep]; end [~,src] = unix(['echo ' src]);% translates "~" alias src(end) = []; [~,x] = unix(['find ' src maxdepth ' -wholename "' src str '" | sort'],'-echo'); if isempty(x) file = []; return % found nothing end xi = strfind(x,sprintf('\n')); assert(xi(end)==length(x),'Expected last character to be newline') j = 1; for n = 1:length(xi) file{n} = x(j:xi(n)-1); j = xi(n)+1; end