Here's a function that searches for files with a given extension. It searches through a chosen parent directory and all of its subfolder at all levels. At the end of the for-loop, you can choose wether the output (matchList) should list just the filenames or the entire path to the files. I recommend using the entire path in case you have duplicate file names.
parentDir = 'C:\Users\foobar\Documents\MATLAB';
ext = 'inf';
folderList = strsplit(genpath(parentDir), ';')';
exp = sprintf('.+\\.%s', ext);
matchList = {};
for i = 1:length(folderList)
folderData = dir(folderList{i});
names = {folderData.name};
strLen = cellfun(@length, names);
endIdxCell = regexp(names, exp, 'end');
endIdx = zeros(size(endIdxCell));
endIdx(~cellfun(@isempty, endIdxCell)) = [endIdxCell{:}];
matchList = [matchList; fullfile(folderList{i}, names(endIdx == strLen & endIdx > 0))'];
end
Best Answer