Last active
December 16, 2015 22:39
-
-
Save zertrin/5509159 to your computer and use it in GitHub Desktop.
Function that searches recursively through all subdirectories of a given directory, collecting a list of all file names matching a given regexp pattern it finds. ( from http://stackoverflow.com/questions/2652630/how-to-get-all-files-under-a-specific-directory-in-matlab )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fileList = getAllFiles(dirName, pattern) | |
% Example: filelistCSV = getAllFiles(somePath,'\d+_\d+_\d+\.csv'); | |
dirData = dir(dirName); %# Get the data for the current directory | |
dirIndex = [dirData.isdir]; %# Find the index for directories | |
fileList = {dirData(~dirIndex).name}'; %'# Get a list of the files | |
if ~isempty(fileList) | |
fileList = cellfun(@(x) fullfile(dirName,x),... %# Prepend path to files | |
fileList,'UniformOutput',false); | |
matchstart = regexp(fileList, pattern); | |
fileList = fileList(~cellfun(@isempty, matchstart)); | |
end | |
subDirs = {dirData(dirIndex).name}; %# Get a list of the subdirectories | |
validIndex = ~ismember(subDirs,{'.','..'}); %# Find index of subdirectories | |
%# that are not '.' or '..' | |
for iDir = find(validIndex) %# Loop over valid subdirectories | |
nextDir = fullfile(dirName,subDirs{iDir}); %# Get the subdirectory path | |
fileList = [fileList; getAllFiles(nextDir, pattern)]; %# Recursively call getAllFiles | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment