from
compareRepeatedFolders
by moshik hatsav
Finds all folders with the same name under a specified folder, and compares their content
|
| findDoubles(homeDir) |
% function folderNames = findDoubles(homeDir)
% Arguments: homeDir - The root folder, where the search is done.
% Ignore this argument to use the current dir (pwd)
% Looks for folders with the same name under the given homeDir folder
% The returned structure holds the information about the multiple folders:
% Each field name is the (truncated) name of a multiple folder found
% The content of the field is a cell holding all the paths, where that
% folder was found
%
% Example - the result of running over the following folders structure:
% ./YYY
% ./XXX
% ./XXX/YYY
%
% >> folders=findDoubles('.')
% folders =
% YYY: {'./XXX/YYY' './YYY'}
function folderNames = findDoubles(homeDir)
if ~exist('homeDir', 'var')
homeDir = pwd
end
p = 1;
iFolder=1;
allFolders={}; % holds truncated folder names
allFoldersFull={}; % holds full folder paths
% generate all paths under the homeDir
s=genpath(homeDir);
sl = length(s);
while 1
% get the next path
t = strtok(s(p:end), pathsep);
sl = length(s);
% if it is empty, we have finished
if isempty(t)
break;
else
% advance p to next token
p = p+length(t)+1;
% get the folder name without path
seps=find(t==filesep);
if isempty(seps)
curFolder = t;
else
curFolder = t(seps(length(seps))+1:length(t));
end
% if this folder-name is not yet listed
% add it to the list of all folders
foundFolder = find(cellfun(@(x) strcmp(x,curFolder), allFolders)==1);
if isempty(foundFolder)
% truncated folder name
allFolders{iFolder} = curFolder;
% full path
allFoldersFull{iFolder} = t;
iFolder = iFolder+1;
else
% This folder name was already found
% add it to the double-folders list
% if the folder is not listed here, add a relevant field name
if ~exist('folderNames', 'var') | ~isfield(folderNames, curFolder)
%add it as a new field
folderNames.(curFolder) = allFoldersFull(foundFolder(1));
end
% add the new path to the field
folderNames.(curFolder){length(folderNames.(curFolder))+1}=t;
end
end
end
|
|
Contact us at files@mathworks.com