You are on page 1of 2

function emptydir=deleteemptyfolders(dir1,preview,ignorehiddenfiles)

if nargin<1
dir1='H:\Backup\ToSort\_String EnsamblesARGH';
dir1='H:\Backup\ToSort\_Strings and PianoARGH';
dir1='H:\Backup\ToSort\_PianoARGH';
dir1='H:\Backup\ToSort\_Voice and PianoARGH';
dir1='H:\Backup\ToSort\_Big BandARGH';
dir1='H:\Backup\ToSort\_Brass QuintetARGH';
dir1='H:\Backup\ToSort\Spartiti classica';
dir1='H:\Backup\ToSort\Partituras Brasil-Portugal';
dir1='H:\Backup\ToSort\Orchestra';
dir1='H:\Backup\ToSort\OBOE Y CORNO INGLES';
dir1='E:\Backup\Bandarluno';
deleteemptyfolders(dir1,true,true);
%deleteemptyfolders(dir1,false,true);
return
end
if nargin<3
ignorehiddenfiles=false;
end
if nargin<2
preview=true;
end
emptydir=false;
dirs1=recursivedir(dir1,1,1,0);
for ii=1:numel(dirs1)
%For each subdirectory
if deleteemptyfolders(dirs1(ii).name,preview,ignorehiddenfiles)
%Now the directory is empty: remove it too!
if ~preview
disp([' #' datestr(now) ': ' 'Deleting ' dirs1(ii).name]);
try
rmdir(dirs1(ii).name,'s');
catch
disp(['#' datestr(now) ': ' 'ERROR del. ' dirs1(ii).name]);
end
else
disp([' #' datestr(now) ': ' 'Empty folder ' dirs1(ii).name]);
end
end
end
%After deleting empty subfolders, check whether the original folder is
%empty too:
dirs1=recursivedir(dir1,1,0,0);
if numel(dirs1)==0
%Yes, empty!
emptydir=true;
else
%Not empty: check whether hidden files are to be ignored
if ~ignorehiddenfiles
%No: this folder contains something and even if these are hidden
%files we want to retain them
emptydir=false;
else
%We want to ignore hidden files and consider them as empty
%Try if all the contained files are hidden
for ii=1:numel(dirs1)
[~,att]=fileattrib(dirs1(ii).name);
if ~att.hidden
break
end
end
%Since at least one element is in dirs1, then att.hidden is set
if ~att.hidden
%Found at least one file which is not hidden
emptydir=false;
else
%No file is not hidden: all files are hidden and we want to
%ignore them: the directory is empty
emptydir=true;
end
end
end

You might also like