Wednesday, July 17, 2013

Copy folders and files - example using LINQ

Their are many examples available to copy entire content of a folder to another folder also retaining its file structure. Here I tried to do the same just with a different flavor. I used LINQ which is new and I haven't came across anyone who used it's power.

Concept is very simple with following steps:
  1. Get list of all files with complete path.
  2. Check each file and determine if corresponding folder structure exists? if not then create the folder.
  3. Copy the relevant file to destination location
Created simple console application for reference and use it as per your requirement.

namespace CopyFilesLINQ
    class Program
        static void Main(string[] args)
            string src = @"D:\Adobe_Photoshop_CS3";
            string dest = @"d:\TestCopy";
            string DD;
            String[] saFiles = Directory.GetFiles(src, "*.*", SearchOption.AllDirectories);
            double dlen = saFiles.Length, dcnt = 0.00;
            foreach (var file in saFiles)
                string[] x = ((from f in dest.Split(Path.DirectorySeparatorChar) 
                               select f).Union(from s in file.Split(Path.DirectorySeparatorChar) 
                                               where !src.Split(Path.DirectorySeparatorChar).Contains(s) 
                                               select s)).ToArray();
                for (int i = 0; i < (x.Length - 1); i++)
                    if (i == 0)
                        DD = x[i];
                        DD = string.Format(@"{0}{1}{2}", DD, Path.DirectorySeparatorChar.ToString(), x[i]);
                    if (!Directory.Exists(DD))
                    File.Copy(file, string.Join(Path.DirectorySeparatorChar.ToString(), x));
                catch (Exception e)
                    // record the exception in trace log or in a list to refer it later or display
                Console.WriteLine(string.Format("Copy progress {0} %", ((dcnt / dlen) * 100).ToString("0.00")));
            Console.SetCursorPosition(1, 0);
            Console.WriteLine("file Copy successful");


  1. Hey, this day is too much good for me, since this time I am reading this enormous informative article here at my home. Thanks a lot for massive hard work. Produce Password For Folder Free Download PC - Don't Waste Time Looking, Study Information On PC Desktops On this page password protect specific files on iphone

  2. FM Whatsapp mod apk download is a MOD of WhatsApp for Android that adds functions to the original development in terms of privacy and file transfers.

  3. If you are in Jaipur and need beautiful Jaipur Call Girls or white skin Jaipur Call Girls then you can simply book call girls in Jaipur from one of the best escort agency named Jaipur Call Girls. It is No.1 premier escort agency and provide both incall or outcall facility to clients.Please click on the following link to check the official websites Jaipur Call Girls

  4. I loved as much as you will receive carried out right here. The sketch is attractive, your authored subject matter stylish. Nonetheless, you command get got an impatience over that you wish were delivered the following. Unwell unquestionably comes further formerly again at exactly the same nearly a lot often in case your shield this increase. Get more: Top 4 Quotes On Encryption Software & 4 Steps To How To Encrypt A Password For Free 4 Times Better Than Before