I made this class to sync files between two directories (C# .Net 3.5)

Super dad movies 2 Comments »

using

 System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

 

namespace DeployUtil

{

    public class CopyUtil

    {

        public static void Sync(string sourcePath, string destinationPath)

        {

            bool dirExisted = DirExists(destinationPath);

 

            //get the source files

            string[] srcFiles = Directory.GetFiles(sourcePath);

 

            foreach (string sourceFile in srcFiles)

            {

                FileInfo sourceInfo = new FileInfo(sourceFile);

                string destFile = Path.Combine(destinationPath, sourceInfo.Name);

 

                if (!dirExisted && File.Exists(destFile))

                {

                    FileInfo destInfo = new FileInfo(destFile);

                    if (sourceInfo.LastWriteTime > destInfo.LastWriteTime)

                    {

                        //file is newer, so copy it

                        File.Copy(sourceFile, Path.Combine(destinationPath, sourceInfo.Name), true);

                        Console.WriteLine(sourceFile + " copied (newer version)");

                    }

                }

                else

                {

                    File.Copy(sourceFile, Path.Combine(destinationPath, sourceInfo.Name));

                    Console.WriteLine(sourceFile + " copied");

                }

            }

 

            DeleteOldDestinationFiles(srcFiles, destinationPath);

 

            //now process the directories if exist

            string[] dirs = Directory.GetDirectories(sourcePath);

 

            foreach (string dir in dirs)

            {

                DirectoryInfo dirInfo = new DirectoryInfo(dir);

 

                //recursive do the directories

                Sync(dir, Path.Combine(destinationPath, dirInfo.Name));

            }

        }

 

        private static bool DirExists(string path)

        {

            //create destination directory if not exist

            if (!Directory.Exists(path))

            {

                Directory.CreateDirectory(path);

                return true;

            }

            else

            {

                return false;

            }

        }

 

        private static void DeleteOldDestinationFiles(string[] sourceFiles, string destinationPath)

        {

            //get the destination files

            string[] dstFiles = Directory.GetFiles(destinationPath);

 

            foreach (string dstFile in dstFiles)

            {

                FileInfo f = new FileInfo(dstFile);

 

                string[] found = Array.FindAll(sourceFiles, str => GetName(str).Equals(f.Name));

                if (found.Length == 0)

                {

                    //delete file if not found in destination

                    File.Delete(dstFile);

                    LogInfo(dstFile + " deleted");

                }

            }

        }

 

        private static string GetName(string path)

        {

            FileInfo i = new FileInfo(path);

            return i.Name;

        }

 

        private static void LogInfo(string text)

        {

            ConsoleColor was = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Magenta;

            Console.WriteLine(text);

            Console.ForegroundColor = was;

        }

 

    }

}

Posted via email from wiibart’s posterous


Because we beat Brazil I’ve just cut of off all my hair and you should do too

Super dad movies 1 Comment »
Picture #152

1 Hour 7 Minutes AgoPicture #152 out of 152

cut it all off..

  • wiibart

    #1

    wiibart

    wiibart commented with a picture:

    do it?

    1 Hour 5 Minutes Ago

  • wiibart

    #2

    wiibart

    wiibart commented with a picture:

    keep it like this?

    57 Minutes Ago

  • wiibart

    #3

    wiibart

    wiibart commented with a picture:

    yeahhh

    48 Minutes 33 Seconds Ago

  • wiibart

    #4

    wiibart

    wiibart commented with a picture:

    It’s a new me!!

    36 Minutes 26 Seconds Ago

  • wiibart

    #5

    wiibart

    wiibart commented with a picture:

    done !

    19 Minutes 2 Second Ago

Everyone should support the dutch team

Do like me, cut of all your hair!

Posted via email from wiibart’s posterous


Welkom quatro bomen in het Grachtenrijk park

Super dad movies 4 Comments »
Vanmorgen neergezet, hopenlijk vinden ze hun nieuwe habitat plezierig

Posted via email from grachtenrijk’s posterous


test

Super dad movies 4 Comments »
Dit is een test artikel

Posted via email from grachtenrijk’s posterous


Finished soldering the Optoma PK102 pico projector power cable

Super dad movies 4 Comments »
The tools

Connecting the wires

The fire

Charging!

Posted via email from wiibart’s posterous


I have 777 unread messages, isn’t that wicked? @djsteen

Super dad movies 6 Comments »

things are getting out of hand, too many things in this life

Posted via web from wiibart’s posterous


WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in