比较MATLAB中两个文件夹的内容

时间:2016-04-17 18:22:42

标签: arrays matlab cell

我写了一个函数compareTGZ用于比较两个tgz文件夹。 tgz文件夹包含以下类型的文件: - mat文件和文本文件,例如.m.ddf.txt

该功能定义如下:

function [testStatus, testMessage] = compareTGZ(refTGZFile, newTGZFile)

我想添加一个条件来检查refTGZFile中但不在newTGZFile中的文件,反之亦然。

if lenOffnames_old > lenOffnames_new || lenOffnames_old < lenOffnames_new
    for i=1:lenOffnames_old
    % Split the path of fnames_old with delimiter filesep
        refTGZParts = strsplit(fnames_old{i}, filesep);
        % Split the path of fnames_new with delimiter filesep
        newTGZParts = strsplit(fnames_new{i}, filesep);

        if(strcmp(refTGZParts{3},newTGZParts{3}))==0; 
            testStatus = 0;
            % Return files in Reference tgz which are not found in Test tgz
            fprintf('File %s in Reference tgz is not found in Test tgz\n',refTGZParts{3});
            % Return files in Test tgz which are not found in Reference tgz
            fprintf('File %s in Test tgz is not found in Reference tgz\n',newTGZParts{3});
        end
    end

refTGZFile包含的文件多于newTGZFile时,我会得到正确的结果。但newTGZFile包含的文件多于refTGZFile,我收到错误。

请有人就如何解决这个错误向我提出建议。

1 个答案:

答案 0 :(得分:0)

您应该可以使用setdiff轻松确定一个文件而不是另一个文件。

%// Create a temporary directory to untar everything to
tmpdir = tempname;

%// Extract the reference .tgz to this location
fnames_old = untar(refTGZFile, tmpdir);

%// Delete the temporary directory
rmdir(tmpdir, 's')

%// Extract the other .tgz file to the same location
fnames_new = untar(newTGZFile, tmpdir);

%// Use setdiff to compare the files that were in one but not the other
in_old_but_not_new = setdiff(fnames_old, fnames_new);
in_new_but_not_old = setdiff(fnames_new, fnames_old);

%// Clean up the temporary folder
rmdir(tmpdir, 's')

如果您不想将所有内容提取到这样的新位置,并且您有两个绝对路径的列表,则可以将它们转换为相对路径,然后进行比较。

%// Anonymous function to create a relative path
relpath = @(base,pth)regexprep(pth, ['^', regexptranslate('escape', base)], '');

fnames_old = untar(refTGZFile, oldTGZ);
fnames_new = untar(newTGZFile, newTGZ);

%// Convert everything to relative paths
fnames_old_relative = relpath(oldTGZ, fnames_old);
fnames_new_relative = relpath(newTGZ, fnames_new);

%// Compare what is in one but not the other.
is_old_but_not_new = setdiff(fnames_old_relative, fnames_new_relative);
is_new_but_not_old = setdiff(fnames_new_relative, fnames_old_relative);

然后打印出结果

for k = 1:numel(is_old_but_not_new)
    fprintf('File %s in Reference tgz is not found in Test tgz\n', is_old_but_not_new{k});
end

for k = 1:numel(is_new_but_not_old)
    fprintf('File %s in Test tgz is not found in Reference tgz\n', is_new_but_not_old{k});
end