How to loop through all files in subfolders in a main folder?

Hello,
I have a main folder and several subfolders in it. Each subfolder has several files within them. I want to create a loop in such a manner that the code would open a subfolder, do what I want it to do and then move to the next one.
Thanks in advance

 Accepted Answer

Stephen23
Stephen23 on 28 Dec 2018
Edited: Stephen23 on 28 Dec 2018
D = 'full path to the main folder';
S = dir(fullfile(D,'*'));
N = setdiff({S([S.isdir]).name},{'.','..'}); % list of subfolders of D.
for ii = 1:numel(N)
T = dir(fullfile(D,N{ii},'*')); % improve by specifying the file extension.
C = {T(~[T.isdir]).name}; % files in subfolder.
for jj = 1:numel(C)
F = fullfile(D,N{ii},C{jj})
% do whatever with file F.
end
end

8 Comments

Thanks a million. I have had a look at several answers, but this was the easiest one. The code worked perfectly as I desired.
Cheers!
I was using files with extension .zfr in different sub folders. I used this loop to convert those files to .txt. Thank you
Good day and thank you Stephen for the code. It is exctaly what i was looking for.
Please is there any way I can perform the analysis seperate for each sub folder first, then combine the results from each subfolder together later?
For example; I extracted 10 values from each subfolder (containg 10 text files). I would like to calculate the mean value (M) of each sub folder so that I can have M1, M2, M3 .... M20. then calculate the standard deviation of the values M1...M20.
Thanks
What about sub of sub of sub of sub folders?
I'm trying to modify this code but it is finding only the subfolders and not the files, what am I doing wrong?
S = dir(fullfile(RawECGDir,'*'));
visits = setdiff({S([S.isdir]).name},{'.','..'}); % list of subfolders of D.
for ii = 1:numel(visits)
T = dir(fullfile(RawECGDir,visits{ii},'*','C_ecg.cvs')); % improve by specifying the file extension.
C = {T(~[T.isdir]).name}; % files in subfolder.
for jj = 1:numel(C)
file = fullfile(RawECGDir,visits{ii},C{jj});
% do whatever with file F.
end
end
@Emu, try this very well commented demo. Adapt it (from *.bmp) to whatever file extension you want, like .CSV.
% Code to list all sub-folders under some top-level folder.
% function scansubfolders()
clc;
clearvars;
close all;
workspace;
% Ask user to locate some top-level folder.
start_path = pwd;
topLevelFolder = uigetdir(start_path)
if topLevelFolder == 0
return; % User clicked Cancel button.
end
% Generate list of all subfolders.
allSubFolders = dir(fullfile(topLevelFolder, '**/*')); % All files and folders.
allSubFolders = allSubFolders([allSubFolders.isdir]); % Extract only the folders.
allSubFolders = unique({allSubFolders.folder}); % Throw out duplicates.
% Now we have a list of all folders and subfolders as a cell array.
% Optional: display them in the command window.
disp('Here are the subfolders:');
allSubFolders
% Display BMP files from each subfolder.
for k = 1 : length(allSubFolders)
% Get the name of this subfolder:
thisFolder = allSubFolders{k};
fprintf('Now processing folder #%d of %d : "%s".\n', k, length(allSubFolders), thisFolder); % Print to command window what folder we're processing.
% Get the file pattern for the files that we want to process.
filePattern = [thisFolder, '\*.bmp'];
% Find out names of files with this file pattern in this folder.
filesInThisFolder = dir(filePattern); % Cell array
if ~isempty(filesInThisFolder) % If there are files in this folder, do this:
numFiles = numel(filesInThisFolder); % Store the number of files in this one subfolder.
% If there are any files, display and process them all.
for fileNumber = 1 : numFiles
fullFileName = fullfile(thisFolder, filesInThisFolder(fileNumber).name);
fprintf(1, 'About to process #%d of %d in this folder : "%s".\n', fileNumber, numFiles, fullFileName);
% For example, maybe we want to process the file by reading it in as and image and display it.
% You can process it however you want though.
imageArray = imread(fullFileName);
imshow(imageArray);
title(filesInThisFolder(fileNumber).name);
drawnow;
% Optional : Ask user if they want to continue and process the next file.
if fileNumber ~= numFiles
promptMessage = sprintf('Done with #%d of %d in this folder.\nDo you want to Continue with file #%d,\nor Cancel to abort?', fileNumber, numFiles, fileNumber+1);
button = questdlg(promptMessage, 'Continue', 'Continue', 'Cancel', 'Continue');
if contains(button, 'Cancel')
% User wants to quit.
return;
end
end
end
else
fprintf(' Skipping folder #%d because there are no files in it with our pattern.\n', k); % Print to command window what folder we're processing.
end
end
uiwait(helpdlg('Check out the command window for the folder and file names.'));
I used this and https://www.mathworks.com/matlabcentral/answers/5304-get-list-of-subclasses to map out class/subclass structure of a bunch of files. Thank you!

Sign in to comment.

More Answers (0)

Asked:

on 28 Dec 2018

Edited:

on 1 Feb 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!