delete subfolder with specific name*

5 ビュー (過去 30 日間)
Long Hà Vinh
Long Hà Vinh 2018 年 12 月 17 日
編集済み: Stephen23 2018 年 12 月 17 日
I have 1 <main folder> with many subfolder like:
<output_figure_file_1>
<output_figure_file_2>
<output_figure_file_3>
<output_data_file_1>
<output_data_file_2>
<output_data_file_3>
<file_1>
<file_2>
<file_3>
With some file inside subfolder. Now I need to delete all sub-folder named <output_figure_file_*>
with using this code:
close all; clear all; clc;
% remove sub folder on input
d=dir('output_figure_*')
dirlist=d([d.isdir])
dirlist=dirlist(~ismember({dirlist.name},{'.','..'}));
for idir=1:numel(dirlist)
%dird=fullfile(pwd,(dirlist(idir).name))
rmdir((dirlist(idir).name),'s')
end
It caused eror said:
??? Error using ==> rmdir
No directories were removed.
Error in ==> Untitled at 8
rmdir((dirlist(idir).name),'s')
Any one pls help me to fix this eror.
My version is R2008a
  2 件のコメント
Guillaume
Guillaume 2018 年 12 月 17 日
Change the call to rmdir to:
[status, message] = rmdir(dirlist(idir).name, 's');
if ~status
error('Fed to remove %s, because %s', dirlist(idir).name, message);
end
and tell us what the new error message is.
Long Hà Vinh
Long Hà Vinh 2018 年 12 月 17 日
I keep 1 file on opened. So It can not delete the 1st folder. I am so woolgathering. Sorry!

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

採用された回答

Jan
Jan 2018 年 12 月 17 日
Add some error messages:
base = cd;
d = dir(fullfile(base, 'output_figure_*'));
dirlist = d([d.isdir]);
dirlist = dirlist(~ismember({dirlist.name}, {'.','..'}));
for idir = 1:numel(dirlist)
folder = fullfile(base, dirlist(idir).name);
[status, msg] = rmdir(folder, , 's');
if status ~= 1
error('rmdir:failed:now', 'Folder: %s, Message: %s', folder, msg);
end
end
What do you get as message?
Is one of the files opened anywhere?
  2 件のコメント
Long Hà Vinh
Long Hà Vinh 2018 年 12 月 17 日
Dear Jan
You right, I keep 1 file in folder <output_figure_file_1> opened so I can not pass the loop 1. Now I turn it off and it run. Thanks!
Stephen23
Stephen23 2018 年 12 月 17 日
編集済み: Stephen23 2018 年 12 月 17 日
An alternative to if and error is to use assert:
assert(status==1,'rmdir:failed:now', 'Folder: %s, Message: %s', folder, msg)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by