I have a series of folders with unique names but the sub folders have the same names:
Folder 1
Subfolder A
Subfolder B
Folder 2
Subfolder A
Subfolder B
How do I loop through all the folders to delete all of the Subfolder B's?

2 件のコメント

Jan
Jan 2021 年 4 月 2 日
You can do this easily in the explorer also, if you are working with Windows.
Nadeau Hahne
Nadeau Hahne 2021 年 4 月 2 日
編集済み: Nadeau Hahne 2021 年 4 月 2 日
Thank you, I'm struggling to get comfortable with Matlab so I'm trying my best to learn

サインインしてコメントする。

 採用された回答

Jan
Jan 2021 年 4 月 2 日

0 投票

BaseFolder = 'C:\Parent\Folder'
List = dir(fullfile(BaseFolder, '**', 'Folder B'));
List = List([List.isdir]);
List(strcmp({List.name}, '..')) = [];
for k = 1:numel(List)
disp(List(k).folder)
% Check this twice before you uncomment this:
% rmdir(List(k).folder);
end

4 件のコメント

Nadeau Hahne
Nadeau Hahne 2021 年 4 月 2 日
Hi,
Thank you I am working through this now. How does the line below function? What does '..' mean and why are the brackets empty after =
List(strcmp({List.name}, '..')) = [];
Thank you
Nadeau Hahne
Nadeau Hahne 2021 年 4 月 2 日
編集済み: Nadeau Hahne 2021 年 4 月 2 日
Thanks! Almost there, but this definitely works so far. My directories ar enot empy, but I can't figure out syntax for if the directores that I want to get rid of have files in them.
I've read rmdir documentation but I don't understnad how to incorporate it
Jan
Jan 2021 年 4 月 3 日
The '..' is a char vector. DIR replies '.' as pointer to the current folder and '..' for the parent folder.
List(strcmp({List.name}, '..')) = [];
To find out, what this does, evaluate it in parts:
{List.name}
strcmp({List.name}, '..')
List(strcmp({List.name}, '..'))
The " = []" is Matlab's method to delete elements. The complete line removes all elements of the List, if their name is '..' .
If the folders are not empty, ue the 's' flag:
rmdir(List(k).folder, 's');
Nadeau Hahne
Nadeau Hahne 2021 年 4 月 6 日
Thank you!!! Trying to learn quickly and feeling like I'm drinking from a fire hose. Appreciate the help

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

製品

リリース

R2020b

質問済み:

2021 年 4 月 2 日

コメント済み:

2021 年 4 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by