如何比较两个不同文件夹中的文件并执行条件复制

时间:2014-12-07 01:05:31

标签: c# file-io io directory fileinfo

我需要将所有文件从一个文件夹(Source)复制到另一个文件夹(Destination)。 我还需要比较两个文件夹并增加一个计数器,如果两个文件夹的内容与名称完全匹配,则停止在100处。

我不需要比较每个文件的大小,只需要比较名称。

这是我尝试的但是我没有得到如上所述的渴望结果。

我在这里引用了一些资源: http://msdn.microsoft.com/en-us/library/bb546137.aspx

class Program
{
    class FileCompare : System.Collections.Generic.IEqualityComparer<System.IO.FileInfo>
    {
        public FileCompare() { }

        public bool Equals(System.IO.FileInfo f1, System.IO.FileInfo f2)
        {
            return (f1.Name == f2.Name &&
                    f1.Length == f2.Length);
        }
        public int GetHashCode(System.IO.FileInfo fi)
        {
            string s = String.Format("{0}{1}", fi.Name, fi.Length);
            return s.GetHashCode();
        }
    }

    static void Main(string[] args)
    {

        int i = 1;

        string sourcePath = @"C:\Users\Administrator\Desktop\Source";
        string destinationPath = @"C:\Users\Administrator\Desktop\Dest";

        string fileName = System.IO.Path.GetFileName(sourcePath);

        string source = System.IO.Path.Combine(sourcePath, fileName);
        string destination = System.IO.Path.Combine(destinationPath, fileName);

        System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(sourcePath);
        System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(destinationPath);

        IEnumerable<System.IO.FileInfo> list1 = dir1.GetFiles("*.*",
        System.IO.SearchOption.AllDirectories);

        IEnumerable<System.IO.FileInfo> list2 = dir2.GetFiles("*.*",
        System.IO.SearchOption.AllDirectories);


        string[] files = System.IO.Directory.GetFiles(sourcePath);

        foreach (string s in files)
        {
            fileName = System.IO.Path.GetFileName(s);
            destination = System.IO.Path.Combine(destinationPath, fileName);
            System.IO.File.Copy(s, destination, true);

            FileCompare myFileCompare = new FileCompare();
            bool areIdentical = list1.SequenceEqual(list2, myFileCompare);

            while (areIdentical != true)
            {
                Console.WriteLine(i++);
            }

        }

        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

2 个答案:

答案 0 :(得分:8)

您可以尝试此解决方案,同步两个目录。

class Program
{
    class FileCompare : System.Collections.Generic.IEqualityComparer<System.IO.FileInfo>
    {
        public bool Equals(System.IO.FileInfo f1, System.IO.FileInfo f2)
        {
            return (f1.Name == f2.Name);
        }
        public int GetHashCode(System.IO.FileInfo fi)
        {
            string s = fi.Name;
            return s.GetHashCode();
        }
    }

    static void Main(string[] args)
    {
        string sourcePath = @"C:\Users\Administrator\Desktop\Source";
        string destinationPath = @"C:\Users\Administrator\Desktop\Dest";

        System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(sourcePath);
        System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(destinationPath);

        IEnumerable<System.IO.FileInfo> list1 = dir1.GetFiles("*.*",
        System.IO.SearchOption.AllDirectories);

        IEnumerable<System.IO.FileInfo> list2 = dir2.GetFiles("*.*",
        System.IO.SearchOption.AllDirectories);

        bool IsInDestination = false;
        bool IsInSource = false;

        foreach (System.IO.FileInfo s in list1)
        {
            IsInDestination = true;

            foreach (System.IO.FileInfo s2 in list2)
            {
                if (s.Name == s2.Name)
                {
                    IsInDestination = true;
                    break;
                }
                else
                {
                    IsInDestination = false;
                }
            }

            if (!IsInDestination)
            {
                System.IO.File.Copy(s.FullName, System.IO.Path.Combine(destinationPath, s.Name), true);
            }
        }

        list1 = dir1.GetFiles("*.*", System.IO.SearchOption.AllDirectories);

        list2 = dir2.GetFiles("*.*", System.IO.SearchOption.AllDirectories);

        bool areIdentical = list1.SequenceEqual(list2, new FileCompare());

        if (!areIdentical)
        {
            foreach (System.IO.FileInfo s in list2)
            {
                IsInSource = true;

                foreach (System.IO.FileInfo s2 in list1)
                {
                    if (s.Name == s2.Name)
                    {
                        IsInSource = true;
                        break;
                    }
                    else
                    {
                        IsInSource = false;
                    }
                }

                if (!IsInSource)
                {
                    System.IO.File.Copy(s.FullName, System.IO.Path.Combine(sourcePath, s.Name), true);
                }
            }
        }

        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

答案 1 :(得分:-1)

//对于复制,从源到目标的所有内容,甚至目标中缺少的任何特定文件都将被复制。您需要在删除中修改索引

// if(s.FullName.Remove(0,24)== s2.FullName.Remove(0,24)) //我的路径是D:\ TestProgram \,其中包含两个文件夹Prod_bin和Jul-2020 //因此,如果两个位置上都存在两个文件,我将对其进行修剪,以便它将// name和文件位置进行比较。

//文件名D:\ TestProgram \ Prod_BIN \ ABC \ bin \ sanyog.css //D:\TestProgram\JUl-2020\ABC\bin\sanyog.css

//删除后就像ABC \ bin \ sanyog.exe。

//需要验证索引并尝试实现它。

//,还需要替换您的源和目标文件夹名称,例如 //矿山字符串destfilePath = s.FullName.Replace(“ Prod_BIN”,“ JUL-2020”);

class Program
{
    class FileCompare : System.Collections.Generic.IEqualityComparer<System.IO.FileSystemInfo>
    {
        public bool Equals(System.IO.FileSystemInfo f1, System.IO.FileSystemInfo f2)
        {
            return (f1.Name == f2.Name);
        }
        public int GetHashCode(System.IO.FileSystemInfo fi)
        {
            string s = fi.Name;
            return s.GetHashCode();
        }
    }

    static void Main(string[] args)
    {

        int i = 0;

        string sourcePath = @"D:\TestProgram\Prod_BIN";
        string destinationPath = @"D:\TestProgram\JUL-2020";

        System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(sourcePath);
        System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(destinationPath);

        IEnumerable<System.IO.FileSystemInfo> list1 = dir1.GetFileSystemInfos("*.*",
        System.IO.SearchOption.AllDirectories);

        IEnumerable<System.IO.FileSystemInfo> list2 = dir2.GetFileSystemInfos("*.*",
        System.IO.SearchOption.AllDirectories);

        bool IsInDestination = false;

        foreach (System.IO.FileSystemInfo s in list1)
        {
            IsInDestination = true;

            foreach (System.IO.FileSystemInfo s2 in list2)
            {
                if (s.Name == s2.Name)
                {
                    if (s.FullName.Remove(0, 24) == s2.FullName.Remove(0, 24))
                    {
                        IsInDestination = true;
                        break;
                    }
                }
                else
                {
                    IsInDestination = false;
                }
            }
            string destfilePath = s.FullName.Replace("Prod_BIN", "JUL-2020");
            if (!IsInDestination)
            {
                i = i + 1;
                if (s.Attributes.Equals(System.IO.FileAttributes.Directory))
                {
                    Directory.CreateDirectory(destfilePath);
                }
                else
                {
                    if (!s.Attributes.Equals(System.IO.FileAttributes.Hidden | System.IO.FileAttributes.Directory))
                        System.IO.File.Copy(s.FullName, System.IO.Path.Combine(Directory.GetParent(destfilePath).FullName, s.Name), true);
                }
            }
        }
        Console.WriteLine("Total copied files : {0} Press any key to exit.", i);
        Console.ReadKey();

    }
}