필터 지우기
필터 지우기

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

조회 수: 127 (최근 30일)
Gopika Rajan
Gopika Rajan 2018년 12월 28일
편집: Jason Sebek 2023년 2월 1일
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

채택된 답변

Stephen23
Stephen23 2018년 12월 28일
편집: Stephen23 2018년 12월 28일
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
Emu
Emu 2022년 10월 5일
Awesome thank you !
Jason Sebek
Jason Sebek 2023년 2월 1일
편집: Jason Sebek 2023년 2월 1일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by