there are specified folders in an specified directory that contents of some of them are equals to contents of others

there are specified folders in an specified directory that contents of some of them are equals to contents of others. the only difference between these folders is the name of them (other properties like:size,numbers of inside files, inside files names,.. are same) its needed to keep only one of these folder and delete other similar folders.
for example in specified directory there are these folders:a,b,c,d,f,g,h,j,....
a,f,r are equal and also b,t,l,o are equal too. now its needed to keep 'a' and delete 'f,r' and also to keep 'b' and delete t,l,o
i use this code for gathering the names of files that are inside each folder: http://www.mathworks.com/matlabcentral/fileexchange/30835-getcontents
and use these command but it doesn't work correctly
EDITED
Directory='D:\Cl\Clas\'; % this is the same specified directory that i told in the question
FolderNames=get_contents(Directory);%this gives names of folders that i want to comparing them (there are 280 folders)
NumbersOfFolders=size(get_contents(Directory),1);
cd(Directory);
for i=1:NumbersOfFolders
A_dir=dir(FolderNames{i,1});
A_dir=rmfield(A_dir,'date');
for j=1:NumbersOfFolders
B_dir=dir(FolderNames{j,1});
B_dir=rmfield(B_dir,'date');
a=isequal(A_dir, B_dir);
if a==1
rmdir(FolderNames{j,1},'s')
end
end
end
Now there are 2 problems: 1- one of the equals folders must be keep but not has been kept
2- after deleting some folders errors :
??? Error using ==> rmdir
D:\Cl\Clas\foldername is not a directory.
Error in ==> folderdeletingm at 40
rmdir(FolderNames{j,1},'s')
because this folder(FolderNames{i,1}) has been deleted in loop 2 when FolderNames{j,1} = FolderNames{i,1}. also itself is deleted in second loop too and this cause this error

2 Comments

no i downloaded it from here:
http://www.mathworks.com/matlabcentral/fileexchange/30835-getcontents

Sign in to comment.

 Accepted Answer

Directory = 'D:\Cl\Clas\';
FolderNames = get_contents(Directory);
NumbersOfFolders = numel(FolderNames); % Faster
cd(Directory);
for i = 1:NumbersOfFolders
% Exclude '.' and '..':
A_dir = dir(FolderNames{i});
A_name = {A_dir.name};
index = or(strcmp(A_name, '.'), strcmp(A_name, '..'));
A_dir = A_dir(~index);
A_dir = rmfield(A_dir,'date');
for j = (i+1):NumbersOfFolders % (i+1) instead of 1 !!!
if exist(FolderNames{j}, 'dir') == 0 % [EDITED]
continue; % [EDITED]
end % [EDITED]
% Exclude '.' and '..':
B_dir = dir(FolderNames{j});
B_name = {B_dir.name};
index = or(strcmp(B_name, '.'), strcmp(B_name, '..'));
B_dir = B_dir(~index);
B_dir = rmfield(B_dir, 'date');
if isequal(A_dir, B_dir)
rmdir(FolderNames{j}, 's')
end
end
end

5 Comments

so nice
this errors too but by running again and again manually this m-file, result is good
error is same:
??? Error using ==> rmdir
D:\Cl\Clas\foldername is not a directory.
Error in ==> folderdeletingm at 65
rmdir(FolderNames{j}, 's')
See [EDITED]: Added code to catch a missing folder.
and i have another question about rename this folders please see that too Jan ( i m writing it)

Sign in to comment.

More Answers (2)

I suggest you use this:
A_dir=dir(FolderA);
B_dir=dir(FolderB);
Then use isequal(A_dir,B_dir) to compare if all the file properties are equal.
You can use Fangjun's solution and remove the date:
A_dir = dir(FolderA);
B_dir = dir(FolderB);
[A_dir.date] = deal('');
[A_dir.datenum] = deal(0);
[B_dir.date] = deal('');
[B_dir.datenum] = deal(0);
isequal(A_dir, B_dir)
If this is time-critical, you can use FEX: fRMField to remove the date and datenum fields faster.

3 Comments

Thanks Jan
now how could make for-loop to do automatically delete?
when i use loops that i told in the question above, first it deletes equal folders but after deleting, numbers of contents of the directory changes( because for example 5 folders are deleted and number of contents now are 5 numbers less than ago). so 'for i=1:numbers of contents' because of varying of 'numbers of contents' gives error.
I do not understand the problem. The above code is used to compare two folders. If the 2nd euqals the first, the second one is deleted. Please post the updated code (as addition in the original question, not as comment) and append the error message, if an error occurs.

Sign in to comment.

Categories

Asked:

on 19 Sep 2011

Community Treasure Hunt

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

Start Hunting!