Deleting folders with specific name

Hi I have one main folder which is named Newfolder.
C:\Users\sesa498313\Desktop\Newfolder
in this Newfolder there are many folders with different name such as F21802010055, F21802010058, etc. In some of these sub-folders there is a folder name FTC, and I want to delete all these folder at the first step. Can you please advise how I can do that?
Thanks

Réponses (1)

Jan
Jan le 4 Juin 2018
Modifié(e) : Jan le 4 Juin 2018
Base = 'C:\Users\sesa498313\Desktop\Newfolder';
List = dir(fullfile(Base, '**', 'FTC'));
List = List([List.isdir]); % Folders only
for k = 1:numel(List) % [EDITED, typo: list -> List]
[status,msg] = rmdir(fullfile(List(k).folder, List(k).name));
is status ~= 1
fprintf(2, '%s\n', msg);
end
end

7 commentaires

roozbeh yousefnejad
roozbeh yousefnejad le 4 Juin 2018
Modifié(e) : roozbeh yousefnejad le 4 Juin 2018
Thanks Jan, but I get error says
Undefined function or variable 'list'.
Error in del (line 4)
for k = 1:numel(list )
what do you recommend now?
Jan
Jan le 4 Juin 2018
@roozbeh yousefnejad: Come on. You can see the code. I've defined and used the variable "List" before. But in the for loop definition I made a typo by writing "list" with a lower-case L. This is fixed now.
roozbeh yousefnejad
roozbeh yousefnejad le 4 Juin 2018
Modifié(e) : roozbeh yousefnejad le 4 Juin 2018
Sorry ;-) my bad. but still I get nothing. There is no error but List is 0*0 struct, and when I checked the folders, FTC files are still in the folders. Actually I checked now and in all the folders, I still have FTC folders.
@Jan I think there is a problem with dir as I am not getting an answer from it. I went through these steps
>> fullfile(Base, '**', 'FTC')
ans =
'C:\Users\sesa498313\Desktop\del\**\FTC'
>> dir(ans);
'C:\Users\sesa498313\Desktop\del\**\FTC' not found.
I think that's why List is 0*0 struct. Do you have any recommendation for it?
Jan
Jan le 4 Juin 2018
Modifié(e) : Jan le 4 Juin 2018
Which Matlab versions are you using? The recursive search with works since R2016b. Please mention if you are working with an old versions.
If you have an older Matlab version, you can use one of the dozens of implementations from the FileExchange: https://www.mathworks.com/matlabcentral/fileexchange/?utf8=%E2%9C%93&term=recursive+dir
roozbeh yousefnejad
roozbeh yousefnejad le 4 Juin 2018
the version I am using is 2017b
Jan
Jan le 5 Juin 2018
Then dir understands the operation. In this case the message means, that the folder C:\Users\sesa498313\Desktop\del does not contain subfolders, which are called 'FTC'. Are you sure that you have such folders?

Connectez-vous pour commenter.

Produits

Version

R2017b

Commenté :

Jan
le 5 Juin 2018

Community Treasure Hunt

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

Start Hunting!

Translated by