且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

比较两个文件夹具有对称差不完全相同的文件?

更新时间:2023-11-30 20:01:16

我感觉我像你正在寻找不同的东西,真正做到:




  • 该文件存在于一个目录而不是其他的(因此使用对称差)


  • 在那里他们的文件是的存在于目录,但那么:




    • 那些通过长度不同

    • 那些通过上次写入时间不同




我会单独处理这些。你已经知道该怎么做的第一部分。获得第二个部分,你需要两套文件名的交集(只使用了相交扩展方法)。从您可以列出区别:

  VAR differentLengths =从名字路口
让文件1 =新的FileInfo(directory1中,名)
让文件2 =新的FileInfo(directory2,名)
,其中file1.Length!= file2.Length
选择新的{名称=名称,
长度1 = file1.Length,
长度2 = file2.Length};



...然后你可以打印这些的。那么你可以做同样为最后写入时间。



在换句话说,你实际上并不需要它一次比较所有属性的比较器。


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...I have used this code

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 && x.LastWriteTime== y.LastWriteTime;
        }

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

// 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); 

What i need is if length differs i have to write length along with directory name and if name differs i have to write name along with directory name or if lastwritetime differs i have to write lastwritetime along with directory name...Any suggestion??

In this format:

Missed Files detail :
---------------------
File Name           Size         Date       Path    


Discrepancies in File Size:
--------------------------

Size                 Path


Discrepancies in File Date:
--------------------------
date                 path

It feels to me like you're looking for different things, really:

  • Files which are present in one directory but not the other (so use symmetric difference)

  • Files where they are present in both directories, but then:

    • Those which differ by length
    • Those which differ by last write time

I would treat these separately. You already know how to do the first part. To get the second part, you need the intersection of the two sets of filenames (just use the Intersect extension method). From that you can list differences:

var differentLengths = from name in intersection
                       let file1 = new FileInfo(directory1, name)
                       let file2 = new FileInfo(directory2, name)
                       where file1.Length != file2.Length
                       select new { Name = name,
                                    Length1 = file1.Length,
                                    Length2 = file2.Length };

... you can then print those out. You can then do the same for last write times.

In other words, you don't actually need a comparer which compares all of the properties at a time.