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

5 ビュー (過去 30 日間)
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 件のコメント
Fangjun Jiang
Fangjun Jiang 2011 年 9 月 19 日
What is get_contents()? You own function?
mohammad
mohammad 2011 年 9 月 20 日
no i downloaded it from here:
http://www.mathworks.com/matlabcentral/fileexchange/30835-getcontents

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

採用された回答

Jan
Jan 2011 年 9 月 20 日
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 件のコメント
mohammad
mohammad 2011 年 9 月 20 日
it works perfect!
thanks a lot Jan!
very very nice!
mohammad
mohammad 2011 年 9 月 20 日
and i have another question about rename this folders please see that too Jan ( i m writing it)

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

その他の回答 (2 件)

Fangjun Jiang
Fangjun Jiang 2011 年 9 月 19 日
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.
  4 件のコメント
mohammad
mohammad 2011 年 9 月 20 日
Thanks a lot
because of more speed and being enough, how could these be comparing only by name field?

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


Jan
Jan 2011 年 9 月 20 日
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 件のコメント
Jan
Jan 2011 年 9 月 20 日
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.

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

カテゴリ

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