there are specified folders in an specified directory that contents of some of them are equals to contents of others
2 次查看(过去 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 个评论
采纳的回答
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
更多回答(2 个)
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 个评论
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)
3 个评论
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 Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!