开发者

comparing two folders for non identical files with SymmetricDifference

 var dir1Files = dir1.GetFiles("*", SearchOption.AllDirectories);
 var dir2Files = dir2.GetFiles("*", SearchOption.AllDirectories);

 var difference = dir1Files.ToHashSet(); 
 difference.SymmetricExceptWith(dir2Files);

 string[] foo1 = difference.Select(c => c.Name).ToArray();
 File.WriteAllLines(@"d:\log1.txt", foo1);

Here i am comparing two files based on name and writing in a text file... But i need to write name along with directory name like this comparing two folders for non identical files?...

Any Suggestion?

EDIT: I have two folders A and B..inside that two folders lots of folders and files are there... I am comparing these two folders for non identical files with symmetric difference and write the name and directory name into a text file...my problem is symmetric difference is working fine and is writing both non identical file names into a log file...But i have to write file name with that directory name...

this code is working fine

var dir1Files = dir1.GetFiles("*", SearchOption.AllDirectories).Select(x => new { x.Name, x.Length });
var dir2Files = dir2.GetFiles("*", SearchOption.AllDirectories).Select(x => new { x.Name, x.Length });
var difference = dir1Files.ToHashSet();      
difference.SymmetricExceptWith(dir2Files);
string[] foo1 = difference.Select(c => c.Name).ToArray();
File.WriteAllLines(@"d:\log1.txt", foo1); 

Here i can开发者_JAVA技巧t give like this

string[] foo1 = difference.Select(c => c.Name+""+c.DirectoryName).ToArray();


I think the best way to do this would be to write your own IEqualityComparer<FileInfo> implementation that enforced your definition of equality between two files.

public class FileInfoNameLengthEqualityComparer : EqualityComparer<FileInfo>
{
    public override bool Equals(FileInfo x, FileInfo y)
    {
        if (x == y)
            return true;

        if (x == null || y == null)
            return false;

        // 2 files are equal if their names and lengths are identical.
        return x.Name == y.Name && x.Length == y.Length;
    }

    public override int GetHashCode(FileInfo obj)
    {
        return obj == null
               ? 0  : obj.Name.GetHashCode() ^ obj.Length.GetHashCode();
    }
}

And then the rest of it would look something like (untested):

// Construct custom equality-comparer.
var comparer = new FileInfoNameLengthEqualityComparer();

// Create sets of files from each directory.
var sets = new[] { dir1, dir2 }
                 .Select(d => d.GetFiles("*", SearchOption.AllDirectories))
                 .Select(files => new HashSet<FileInfo>(files, comparer))
                 .ToArray();

// Make the first set contain the set-difference as determined 
// by the equality-comparer.
sets[0].SymmetricExceptWith(sets[1]);

// Project each file to its full name and write the file-names
// to the log-file.
var lines = sets[0].Select(fileInfo => fileInfo.FullName).ToArray();
File.WriteAllLines(@"d:\log1.txt", lines); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜