フィルターのクリア

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 Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by