No BSD License  

Highlights from
compareRepeatedFolders

from compareRepeatedFolders by moshik hatsav
Finds all folders with the same name under a specified folder, and compares their content

...
function [list1, list2, diff1, diff2, details] = ...
    compareDirectoriesEngine(dirname1, dirname2)
% [list1, list2, diff1, diff2, details] =
%                       compareDirectoriesEngine(dirname1, dirname2)
% LIST1 and LIST2 are list of files/directories in dir1 and dir2,
% respectively. If an entry does not exist in the directory, an empty
% string is placed. 
% DIFF1 and DIFF2 are cell arrays that indicate differences. 
% No difference is '  ', missing entry is 'x ', different
% file size is 's ', and different modification date is 'd '.
% DETAILS holds a textual definition of the differences
%
% NOTE:
% This method is essentially taken from compdirectory.m by Jiro Doke,
% Matlab File Exchange, November 2006

d1 = dir(dirname1);d1(1:2) = '';
d2 = dir(dirname2);d2(1:2) = '';

% separate into files and directories
d1dirs  = d1([d1.isdir]);
d2dirs  = d2([d2.isdir]);
d1files = d1(~[d1.isdir]);
d2files = d2(~[d2.isdir]);

list1 = {};
list2 = {};
diff1 = {};
diff2 = {};
details = {};

% traverse through all files in dir1
for id1 = 1:length(d1files)
    list1 = [list1;{d1files(id1).name}];
    id2 = strmatch(d1files(id1).name, {d2files.name}, 'exact');
    if isempty(id2)
        diff1 = [diff1;{'x  '}];
        diff2 = [diff2;{'x  '}];
        list2 = [list2;{''}];
        str = sprintf('"%s" does not exist in dir2.', d1files(id1).name);
        details = [details;{str}];
    else
        list2 = [list2;{d2files(id2).name}];
        if d1files(id1).bytes ~= d2files(id2).bytes
            diff1 = [diff1;{' s '}];
            diff2 = [diff2;{' s '}];
            str = sprintf('"%s" has a different file size.', d1files(id1).name);
            details = [details;{str}];
        elseif ~strcmp(d1files(id1).date, d2files(id2).date)
            diff1 = [diff1;{' d '}];
            diff2 = [diff2;{' d '}];
            str = sprintf('"%s" has a different modification date.', d1files(id1).name);
            details = [details;{str}];
        else % same file
            diff1 = [diff1;{''}];
            diff2 = [diff2;{''}];
            details = [details;{''}];
        end
        % remove from the list
        d2files(id2) = '';
    end
end

% traverse through the remainding files in dir2
% these files do not exist in dir1
list2 = [list2;{d2files.name}'];
list1 = [list1;repmat({''},length(d2files),1)];
diff1 = [diff1;repmat({'x  '},length(d2files),1)];
diff2 = [diff2;repmat({'x  '},length(d2files),1)];
for id2 = 1:length(d2files)
    str = sprintf('"%s" does not exist in dir1.', d2files(id2).name);
    details = [details;{str}];
end

% traverse through all directories in dir1
subDirs = [];
for id1 = 1:length(d1dirs)
    id2 = strmatch(d1dirs(id1).name, {d2dirs.name}, 'exact');
    if isempty(id2)
        list1 = [list1;{['\', d1dirs(id1).name]}];
        list2 = [list2;{''}];
        diff1 = [diff1;{'x  '}];
        diff2 = [diff2;{'x  '}];
        str = sprintf('directory "%s" does not exist in dir2.', d1dirs(id1).name);
        details = [details;{str}];
    else
        subDirs = [subDirs; id1, id2];
    end
end

% traverse through the remainding directories in dir2
for id2 = 1:length(d2dirs)
    id1 = strmatch(d2dirs(id2).name, {d1dirs.name}, 'exact');
    if isempty(id1)
        list1 = [list1;{''}];
        list2 = [list2;{['\', d2dirs(id2).name]}];
        diff1 = [diff1;{'x  '}];
        diff2 = [diff2;{'x  '}];
        str = sprintf('directory "%s" does not exist in dir1.', d2dirs(id2).name);
        details = [details;{str}];
    end
end

% recursive comparison of subdirectories
for ii = 1:size(subDirs, 1)
    list1 = [list1;{['\', d1dirs(subDirs(ii, 1)).name]}];
    list2 = [list2;{['\', d2dirs(subDirs(ii, 2)).name]}];
    if ~strcmp(d1dirs(subDirs(ii, 1)).date, d2dirs(subDirs(ii, 2)).date)
        diff1 = [diff1;{' d '}];
        diff2 = [diff2;{' d '}];
        str = sprintf('directory "%s" has a different modification date.', d1dirs(subDirs(ii, 1)).name);
        details = [details;{str}];
    else
        diff1 = [diff1;{''}];
        diff2 = [diff2;{''}];
        details = [details;{''}];
    end
    str = sprintf('%s|', str);
    [l1, l2, ll1, ll2, det1] = compareDirectoriesEngine(...
        fullfile(dirname1, d1dirs(subDirs(ii, 1)).name), ...
        fullfile(dirname2, d2dirs(subDirs(ii, 2)).name));
    if ~isempty(l1)
        list1 = [list1;cellstr([repmat(' ', length(l1), 2), char(l1)])];
        list2 = [list2;cellstr([repmat(' ', length(l2), 2), char(l2)])];
        diff1 = [diff1;ll1];
        diff2 = [diff2;ll2];
        details = [details;det1];
    end
end

Contact us at files@mathworks.com