none
Best way to update files between folders RRS feed

  • Question

  • I have two directories (dirA and dirB), and i want to copy all files from dirA including sub-folders to dirB where a file in dirA replaces a file in dirB only if the two files have the same name (and also the same sub-path) and also fileB.LastWriteTime < fileA.LastWriteTime, or else if a file in dirA is not present in dirB.

    So, if I have these directories:

    <directory Name="dirA">
    
       <file Name="file1" LastWriteTime="10:00" />
       <file Name="file2" LastWriteTime="11:00" />
       <file Name="file7" LastWriteTime="12:00" />
    
       <directory Name="subDir">
          <file Name="file3" LastWriteTime="10:00" />
          <file Name="file4" LastWriteTime="11:00" />
       </directory>
    
       <directory Name="subDir2">
          <file Name="file5" LastWriteTime="10:00" />
          <file Name="file6" LastWriteTime="11:00" />
       </directory>
    
    </directory>
    
    <directory Name="dirB">
    
       <file Name="file1" LastWriteTime="09:00" />
       <file Name="file2" LastWriteTime="13:00" />
    
       <directory Name="subDir">
          <file Name="file3" LastWriteTime="09:00" />
          <file Name="file4" LastWriteTime="12:00" />
       </directory>
    
       <directory Name="BsubDir2">
          <file Name="file5" LastWriteTime="09:00" />
       </directory>
    
    </directory>

    After the update dirB will be:

    <directory Name="dirB">
    
       <file Name="file1" LastWriteTime="10:00" />
       <file Name="file2" LastWriteTime="13:00" />
       <file Name="file7" LastWriteTime="12:00" />
    
       <directory Name="subDir">
          <file Name="file3" LastWriteTime="10:00" />
          <file Name="file4" LastWriteTime="12:00" />
       </directory>
    
       <directory Name="BsubDir2">
          <file Name="file5" LastWriteTime="09:00" />
       </directory>
    
       <directory Name="subDir2">
          <file Name="file5" LastWriteTime="10:00" />
          <file Name="file6" LastWriteTime="11:00" />
       </directory>
    
    </directory>

    Thanks.

    Sunday, May 27, 2012 10:16 PM

Answers

  • Hi Gliderkite,

    Below code will give you listing of all the files in the mentioned directory, will compare and will also give you the common files. For common file, you can add you code to match those files with lastmodified date.

    namespace QueryCompareTwoDirs
    {
        class CompareDirs
        {
            static void Main(string[] args)
            {
                // Create two identical or different temporary folders 
                // on a local drive and change these file paths.
                string pathA = @"C:\TestDir";
                string pathB = @"C:\TestDir2";
                System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(pathA);
                System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(pathB);
                // Take a snapshot of the file system.
                IEnumerable<System.IO.FileInfo> list1 = dir1.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
                IEnumerable<System.IO.FileInfo> list2 = dir2.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
                //A custom file comparer defined below
                FileCompare myFileCompare = new FileCompare();
                // This query determines whether the two folders contain
                // identical file lists, based on the custom file comparer
                // that is defined in the FileCompare class.
                // The query executes immediately because it returns a bool.
                bool areIdentical = list1.SequenceEqual(list2, myFileCompare);
                if (areIdentical == true)
                {
                    Console.WriteLine("the two folders are the same");
                }
                else
                {
                    Console.WriteLine("The two folders are not the same");
                }
                // Find the common files. It produces a sequence and doesn't 
                // execute until the foreach statement.
                var queryCommonFiles = list1.Intersect(list2, myFileCompare);
                if (queryCommonFiles.Count() > 0)
                {
                    Console.WriteLine("The following files are in both folders:");
                    foreach (var v in queryCommonFiles)
                    {
                        Console.WriteLine(v.FullName); //shows which items end up in result list
                    }
                }
                else
                {
                    Console.WriteLine("There are no common files in the two folders.");
                }
                // Find the set difference between the two folders.
                // For this example we only check one way.
                var queryList1Only = (from file in list1
                                      select file).Except(list2, myFileCompare);
                Console.WriteLine("The following files are in list1 but not list2:");
                foreach (var v in queryList1Only)
                {
                    Console.WriteLine(v.FullName);
                }
                // Keep the console window open in debug mode.
                Console.WriteLine("Press any key to exit.");
                Console.ReadKey();
            }
        }
        // This implementation defines a very simple comparison
        // between two FileInfo objects. It only compares the name
        // of the files being compared and their length in bytes.
        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);
            }
            // Return a hash that reflects the comparison criteria. According to the 
            // rules for IEqualityComparer<T>, if Equals is true, then the hash codes must
            // also be equal. Because equality as defined here is a simple value equality, not
            // reference identity, it is possible that two or more objects will produce the same
            // hash code.
            public int GetHashCode(System.IO.FileInfo fi)
            {
                string s = String.Format("{0}{1}", fi.Name, fi.Length);
                return s.GetHashCode();
            }
        }
    }

    Or can can alternatively check this:

    http://msdn.microsoft.com/en-us/library/bb546137.aspx


    Regards, http://shwetamannjain.blogspot.com or https://shwetalodha.wordpress.com/


    Monday, May 28, 2012 5:40 AM
  • Hello,

    There is a framework for this called "Microsoft Sync Framework" which is designed to synchronize different data sources including files/folders, including conflict resolution schemes and some extensibility points.The interesting thing is that you can implement your own sync providers from scratch or based or the ones that already exist (like the file provider is that doesn't fill all your needs)

    There is many examples on the web, you can google it, but you can start with this two articles:

    http://msdn.microsoft.com/en-us/sync/bb887623.aspx

    http://msdn.microsoft.com/en-us/library/ee617386.aspx

    Regards,

    Jhonatan P.


    JPC

    Tuesday, May 29, 2012 2:51 PM

All replies

  • In this case i think you have to run a loop and manually check the name/lastwritetime.


    Resolving n Evolving in C# (http://jeanpaulva.com)

    Monday, May 28, 2012 12:45 AM
  • Hi Gliderkite,

    Below code will give you listing of all the files in the mentioned directory, will compare and will also give you the common files. For common file, you can add you code to match those files with lastmodified date.

    namespace QueryCompareTwoDirs
    {
        class CompareDirs
        {
            static void Main(string[] args)
            {
                // Create two identical or different temporary folders 
                // on a local drive and change these file paths.
                string pathA = @"C:\TestDir";
                string pathB = @"C:\TestDir2";
                System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(pathA);
                System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(pathB);
                // Take a snapshot of the file system.
                IEnumerable<System.IO.FileInfo> list1 = dir1.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
                IEnumerable<System.IO.FileInfo> list2 = dir2.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
                //A custom file comparer defined below
                FileCompare myFileCompare = new FileCompare();
                // This query determines whether the two folders contain
                // identical file lists, based on the custom file comparer
                // that is defined in the FileCompare class.
                // The query executes immediately because it returns a bool.
                bool areIdentical = list1.SequenceEqual(list2, myFileCompare);
                if (areIdentical == true)
                {
                    Console.WriteLine("the two folders are the same");
                }
                else
                {
                    Console.WriteLine("The two folders are not the same");
                }
                // Find the common files. It produces a sequence and doesn't 
                // execute until the foreach statement.
                var queryCommonFiles = list1.Intersect(list2, myFileCompare);
                if (queryCommonFiles.Count() > 0)
                {
                    Console.WriteLine("The following files are in both folders:");
                    foreach (var v in queryCommonFiles)
                    {
                        Console.WriteLine(v.FullName); //shows which items end up in result list
                    }
                }
                else
                {
                    Console.WriteLine("There are no common files in the two folders.");
                }
                // Find the set difference between the two folders.
                // For this example we only check one way.
                var queryList1Only = (from file in list1
                                      select file).Except(list2, myFileCompare);
                Console.WriteLine("The following files are in list1 but not list2:");
                foreach (var v in queryList1Only)
                {
                    Console.WriteLine(v.FullName);
                }
                // Keep the console window open in debug mode.
                Console.WriteLine("Press any key to exit.");
                Console.ReadKey();
            }
        }
        // This implementation defines a very simple comparison
        // between two FileInfo objects. It only compares the name
        // of the files being compared and their length in bytes.
        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);
            }
            // Return a hash that reflects the comparison criteria. According to the 
            // rules for IEqualityComparer<T>, if Equals is true, then the hash codes must
            // also be equal. Because equality as defined here is a simple value equality, not
            // reference identity, it is possible that two or more objects will produce the same
            // hash code.
            public int GetHashCode(System.IO.FileInfo fi)
            {
                string s = String.Format("{0}{1}", fi.Name, fi.Length);
                return s.GetHashCode();
            }
        }
    }

    Or can can alternatively check this:

    http://msdn.microsoft.com/en-us/library/bb546137.aspx


    Regards, http://shwetamannjain.blogspot.com or https://shwetalodha.wordpress.com/


    Monday, May 28, 2012 5:40 AM
  • Hello,

    There is a framework for this called "Microsoft Sync Framework" which is designed to synchronize different data sources including files/folders, including conflict resolution schemes and some extensibility points.The interesting thing is that you can implement your own sync providers from scratch or based or the ones that already exist (like the file provider is that doesn't fill all your needs)

    There is many examples on the web, you can google it, but you can start with this two articles:

    http://msdn.microsoft.com/en-us/sync/bb887623.aspx

    http://msdn.microsoft.com/en-us/library/ee617386.aspx

    Regards,

    Jhonatan P.


    JPC

    Tuesday, May 29, 2012 2:51 PM