Removing subfolders with same name

조회 수: 12 (최근 30일)
Nadeau Hahne
Nadeau Hahne 2021년 4월 2일
댓글: Nadeau Hahne 2021년 4월 6일
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일
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
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개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by