mostlylucid

STATIC ARCHIVE of mostlylucid.co.uk of old
posts - 895, comments - 679, trackbacks - 11

My Links

News

Archives

Post Categories

Misc. Coding

Merging Directories...dumb little code sample

As I've posted about a few times recently I've moved back to a far more technical role and as a result I've been goofing around with some code...The first thing (which I can talk about ;-)) is a little directory merging utility I've been working on...pretty simple but it was fun to play with. You can find it here if you're interested...along with the main bit of source below...more interesting stuff to come (a lot of the stuff I've been writing is using pre-release bits so I will put it here when you can actually use it!)

using System;

using System.Collections.Generic;

using System.Collections;

using System.Collections.Specialized;

using System.Text;

using System.IO;

 

namespace MergeDirs

{

    class Program

    {

 

        /* Sample code for default config creation

        * Config cfg = new Config() { SourceDirectory = sourceDir, DestinationDirectory = destDir, ClearDestination = true, ExtensionsToExclude = new List<string>(new string[] { "vssscc", "dll", "pdb", "vspscc" }), DirectoriesToExclude = new List<string>(new string[] { "bin" }) };

            cfg.Serialize(@"C:\TestConfig\Config.xml");

            return;

            */

        private Config Cfg { get; set; }

        private const string CONFIG_FILE = @"C:\TestConfig\Config.xml";

        static void Main(string[] args)

        {           

            Program pgm = new Program();

            pgm.Run(args);

        }

 

 

        public void Run(string[] args)

        {

            Cfg = new Config();

            if(args.Length==0)

            {

                Cfg.Deserialize(CONFIG_FILE);

            }

            if (Cfg == null)

            {

                Console.WriteLine("No config file at " + CONFIG_FILE + " and no arguments...I can't guess!");

                Console.ReadLine();

                return;

            }

 if(!string.IsNullOrEmpty(Cfg.SourceDirectory) && !string.IsNullOrEmpty(Cfg.DestinationDirectory))

            {

                if (Directory.Exists(Cfg.SourceDirectory) && Directory.Exists(Cfg.DestinationDirectory))

                {

                    CompareDirs(Cfg.SourceDirectory, Cfg.DestinationDirectory, Cfg.ClearDestination, Cfg.ExtensionsToExclude, Cfg.DirectoriesToExclude);

                }

                else

                {

                    Console.WriteLine("Both source and destination directories must already exist!!");

                }

            }

            else

            {

                Console.WriteLine("You must specify both source and destination directories!");

            }

            Console.WriteLine("Finished!");

            Console.ReadLine();

        }

 

 

        private bool CompareDirs(string sourceDir, string destDir, bool clearRight, List<string> extensionsToExclude, List<string> directoriesToExclude)

        {

               List<string> srcFileNames = new List<string>(GetFSItems(new DirectoryInfo(sourceDir).GetFiles()));

               List<string> dstFileNames = new List<string>(GetFSItems(new DirectoryInfo(destDir).GetFiles()));

               foreach(string fileName in srcFileNames)

               {

                   Console.WriteLine(string.Format("Comparing:{0} and {1}", Path.Combine(sourceDir, fileName), Path.Combine(destDir, fileName)));

                   bool excludeFile = false;

                   foreach (string excludeExt in extensionsToExclude)

                   {

                       if (fileName.EndsWith("." + excludeExt))

                       {

                           excludeFile = true;

 

                           break;

                       }

                   }

                   if (excludeFile)

                       continue;

                       if (!dstFileNames.Contains(fileName))

                       {

                           File.Copy(Path.Combine(sourceDir, fileName), Path.Combine(destDir, fileName));

                       }

                       else

                       {

                           DateTime srcInf = new FileInfo(Path.Combine(sourceDir, fileName)).LastWriteTime;

                           DateTime dstInf = new FileInfo(Path.Combine(destDir, fileName)).LastWriteTime;

                           if (dstInf != srcInf)

                               File.Copy(Path.Combine(sourceDir, fileName), Path.Combine(destDir, fileName), true);

                       }

 

               }

               foreach (string fileName in dstFileNames)

               {

                   if (!srcFileNames.Contains(fileName))

                   {

                       File.Delete(Path.Combine(destDir, fileName));

                   }

               }

            List<string> sourceNames = new List<string>(GetFSItems(new DirectoryInfo(sourceDir).GetDirectories()));

           List<string> destNames = new List<string>(GetFSItems(new DirectoryInfo(destDir).GetDirectories()));

            foreach (string dirName in sourceNames)

            {

                if (directoriesToExclude.Contains(dirName))

                {

                    Console.WriteLine("Skipping: " + dirName);

                    continue;

                }

                Console.WriteLine(string.Format("Comparing:{0} and {1}",Path.Combine(sourceDir, dirName),Path.Combine(destDir, dirName)));

               if (!destNames.Contains(dirName))

               {

                   string newDestDir = Path.Combine(destDir, dirName);

                   Directory.CreateDirectory(newDestDir);

                   CompareDirs(Path.Combine(sourceDir, dirName), newDestDir, clearRight, extensionsToExclude, directoriesToExclude);

               }

               else

               {

                   CompareDirs(Path.Combine(sourceDir, dirName), Path.Combine(destDir, dirName), clearRight, extensionsToExclude, directoriesToExclude);

               }

            }

 

           foreach (string dirName in destNames)

           {

               if (!sourceNames.Contains(dirName))

               {

                   Directory.Delete(Path.Combine(destDir, dirName));

               }

           }

            return false;

        }

 

        private IEnumerable<string> GetFSItems(FileSystemInfo[] dirInfos)

        {

            foreach(FileSystemInfo dinf in dirInfos)

            {

                yield return dinf.Name;

            }

        }

 

    }

}

Incidentally using the CopySourceAsHtml 2008 version from here

Print | posted on Monday, March 31, 2008 2:27 PM | Filed Under [ Code Snippets ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 4 and 1 and type the answer here:

Powered by: