function f = matchfiles(patterns,sorttype) <patterns> is (1) a string that matches zero or more files or directories (wildcards '*' okay) (2) the empty matrix [] (3) a cell vector of zero or more things like (1) or (2) <sorttype> (optional) is how to sort in each individual match attempt. 't' means sort by time (newest first) 'tr' means sort by time (oldest first) default is [], which means to sort alphabetically by explicitly using MATLAB's sort function. (note that MATLAB's sort function may sort differently than UNIX's ls function does!) return a cell vector of strings containing paths to the matched files and/or directories. if there are no matches for an individual match attempt, we issue a warning. this function should be fully functional on Mac and Linux. however, on Windows, we have the following limitations: - you cannot use the '?' operator - you can use the '*' operator only once and at the end of the expression (not in an intermediate directory) on Mac and Linux, if we run into the too-many-files limitation of the ls command, we will resort to the alternative mode described above, and this inherits the same limitations. history: 2011/09/28 - if ls returns too many files, resort to alternative. also, the alternative mode now allows sorttype to be specified. 2011/08/07 - allow empty matrix as an input 2011/04/02 - now, works on Windows (in a limited way) 2011/04/02 - oops, time-sorting behavior did not work. bad bug!!! 2011/02/24 - escape spaces in patterns using \. this fixes buggy behavior. 2011/01/21 - explicitly use MATLAB's sort function to ensure consistency across platforms.
0001 function f = matchfiles(patterns,sorttype) 0002 0003 % function f = matchfiles(patterns,sorttype) 0004 % 0005 % <patterns> is 0006 % (1) a string that matches zero or more files or directories (wildcards '*' okay) 0007 % (2) the empty matrix [] 0008 % (3) a cell vector of zero or more things like (1) or (2) 0009 % <sorttype> (optional) is how to sort in each individual match attempt. 0010 % 't' means sort by time (newest first) 0011 % 'tr' means sort by time (oldest first) 0012 % default is [], which means to sort alphabetically by explicitly using MATLAB's sort function. 0013 % (note that MATLAB's sort function may sort differently than UNIX's ls function does!) 0014 % 0015 % return a cell vector of strings containing paths to the matched files and/or directories. 0016 % if there are no matches for an individual match attempt, we issue a warning. 0017 % 0018 % this function should be fully functional on Mac and Linux. however, on Windows, 0019 % we have the following limitations: 0020 % - you cannot use the '?' operator 0021 % - you can use the '*' operator only once and at the end of the expression 0022 % (not in an intermediate directory) 0023 % 0024 % on Mac and Linux, if we run into the too-many-files limitation of the ls command, 0025 % we will resort to the alternative mode described above, and this inherits the 0026 % same limitations. 0027 % 0028 % history: 0029 % 2011/09/28 - if ls returns too many files, resort to alternative. also, the alternative mode now allows sorttype to be specified. 0030 % 2011/08/07 - allow empty matrix as an input 0031 % 2011/04/02 - now, works on Windows (in a limited way) 0032 % 2011/04/02 - oops, time-sorting behavior did not work. bad bug!!! 0033 % 2011/02/24 - escape spaces in patterns using \. this fixes buggy behavior. 0034 % 2011/01/21 - explicitly use MATLAB's sort function to ensure consistency across platforms. 0035 0036 % input 0037 if ~exist('sorttype','var') || isempty(sorttype) 0038 sorttype = ''; 0039 end 0040 if ~iscell(patterns) 0041 patterns = {patterns}; 0042 end 0043 % % if ~isunix 0044 % % assert(isempty(sorttype),'due to current implementation limitations, <sorttype> must be [] on Windows'); 0045 % % end 0046 0047 % do it 0048 f = {}; 0049 for p=1:length(patterns) 0050 if isempty(patterns{p}) 0051 continue; 0052 end 0053 0054 % if UNIX, try to use ls 0055 doalternative = 0; 0056 if isunix 0057 [status,result] = unix(sprintf('/bin/ls -1d%s %s',sorttype,regexprep(patterns{p},' ','\\ '))); 0058 if status==126 % oops, too many files 0059 doalternative = 1; 0060 elseif status~=0 0061 warning(sprintf('failure in finding the files or directories for %s',patterns{p})); 0062 else 0063 temp = strsplit(result,sprintf('\n')); 0064 temp = temp(~cellfun(@(x) isempty(x),temp)); % remove empty entries 0065 if isempty(sorttype) 0066 temp = sort(temp); 0067 end 0068 f = [f temp]; 0069 end 0070 end 0071 0072 % if not UNIX or if we failed by matching too many files using ls, we have to do the alternative 0073 if ~isunix || doalternative 0074 if exist(patterns{p},'dir') 0075 f = [f {patterns{p}}]; 0076 else 0077 tempdir = stripfile(patterns{p}); 0078 dmatch = dir(patterns{p}); 0079 if isempty(dmatch) 0080 warning(sprintf('failure in finding the files or directories for %s',patterns{p})); 0081 else 0082 if isequal(sorttype,'t') 0083 [ss,ii] = sort(cat(2,dmatch.datenum),2,'descend'); 0084 elseif isequal(sorttype,'tr') 0085 [ss,ii] = sort(cat(2,dmatch.datenum)); 0086 else 0087 [ss,ii] = sort(cat(2,{dmatch.name})); 0088 end 0089 dmatch = dmatch(ii); 0090 temp = cat(2,{dmatch.name}); 0091 temp = temp(~cellfun(@(x) isempty(x),temp)); % remove empty entries 0092 temp = temp(~cellfun(@(x) isequal(x(1),'.'),temp)); % remove things starting with . 0093 if ~isempty(tempdir) 0094 temp = cellfun(@(x) [tempdir x],temp,'UniformOutput',0); % add directory 0095 end 0096 f = [f temp]; 0097 end 0098 end 0099 end 0100 end