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

3 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Jan
Jan il 20 Set 2011
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 Commenti
mohammad
mohammad il 20 Set 2011
and i have another question about rename this folders please see that too Jan ( i m writing it)

Accedi per commentare.

Più risposte (2)

Fangjun Jiang
Fangjun Jiang il 19 Set 2011
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 Commenti
mohammad
mohammad il 20 Set 2011
Thanks a lot
because of more speed and being enough, how could these be comparing only by name field?

Accedi per commentare.


Jan
Jan il 20 Set 2011
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 Commenti
Jan
Jan il 20 Set 2011
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.

Accedi per commentare.

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by