How to loop through all files in subfolders in a main folder?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
1 vote
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
6 votes
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
Gopika Rajan
on 29 Dec 2018
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!
Suhas Nuggehalli Sampathkumar
on 6 Dec 2019
I was using files with extension .zfr in different sub folders. I used this loop to convert those files to .txt. Thank you
AUWAL ABUBAKAR
on 29 Jan 2020
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
Royi Avital
on 30 Jun 2020
What about sub of sub of sub of sub folders?
Emu
on 29 Sep 2022
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
Image Analyst
on 29 Sep 2022
Edited: Image Analyst
on 29 Sep 2022
@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.'));
Emu
on 5 Oct 2022
Awesome thank you !
Jason Sebek
on 1 Feb 2023
Edited: Jason Sebek
on 1 Feb 2023
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!
More Answers (0)
Categories
Find more on Whos in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)