Home > utility > recursiveFileList.m

recursiveFileList

PURPOSE ^

RecursiveFileList: List files with a given extension recursively

SYNOPSIS ^

function allData=recursiveFileList(directoryList, ext, maxFileNumInEachDir)

DESCRIPTION ^

 RecursiveFileList: List files with a given extension recursively
    Usage: allData=recursiveFileList(directoryList, ext, maxFileNumInEachDir)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function allData=recursiveFileList(directoryList, ext, maxFileNumInEachDir)
0002 % RecursiveFileList: List files with a given extension recursively
0003 %    Usage: allData=recursiveFileList(directoryList, ext, maxFileNumInEachDir)
0004 
0005 %    Roger Jang, 20030316
0006 
0007 if nargin<1, error('Need at least one input argument!'); end
0008 if nargin<3, maxFileNumInEachDir=inf; end
0009 if isstr(directoryList); directoryList={directoryList}; end
0010 
0011 allData=[];
0012 for k=1:length(directoryList)
0013     directory=directoryList{k};
0014 
0015     % Get files in the given directory
0016     if (directory(end)=='/') | (directory(end)=='\'); directory(end)=[]; end
0017 
0018     data=dir([directory, '/*.', ext]);
0019     data=data(1:min(length(data), maxFileNumInEachDir));
0020 
0021     for i=1:length(data)
0022         data(i).path=[directory, '/', data(i).name];
0023         [parentPath, junk, junk, junk]=fileparts(data(i).path);
0024         [junk, data(i).parentDir, junk, junk]=fileparts(parentPath);
0025     end
0026     
0027     % Get files in sub-directories
0028     subdirs=dir(directory);
0029     subdirs=subdirs([subdirs.isdir]);    % Get directories only
0030     for i=1:length(subdirs),
0031         if strcmp(subdirs(i).name, '.'), continue; end
0032         if strcmp(subdirs(i).name, '..'), continue; end
0033         thisPath=[directory, '/', subdirs(i).name];
0034         data2=feval(mfilename, thisPath, ext, maxFileNumInEachDir);
0035         if length(data2)==0; data2=[]; end
0036         if length(data)==0; data=[]; end
0037         data=[data; data2];
0038     end
0039     allData=[allData; data];
0040 end

Generated on Tue 01-Jun-2010 09:50:34 by m2html © 2003