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];
                    else
                        DD = string.Format(@"{0}{1}{2}", DD, Path.DirectorySeparatorChar.ToString(), x[i]);
                    if (!Directory.Exists(DD))
                        Directory.CreateDirectory(DD);
                }
                try
                {
                    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
                }
                dcnt++;
                
                Console.SetCursorPosition(1,0);
                Console.WriteLine(string.Format("Copy progress {0} %", ((dcnt / dlen) * 100).ToString("0.00")));
            }
            Console.SetCursorPosition(1, 0);
            Console.WriteLine("file Copy successful");
            Console.ReadLine();
        }
    }
}

11 comments:

  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

    ReplyDelete
  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.

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  5. No I do not need any service, I have the clash of clans mod apk and I use their service.

    ReplyDelete
  6. Hire Best Packers And Movers Mumbai for hassle-free Household Shifting, ***Office Relocation, ###Car Transporation, Loading Unloading, packing Unpacking at affordable ✔✔✔ Price Quotation. Top Rated, Safe and Secure Service Providers who can help you with 24x7 and make sure a Untroubled Relocation Services at Cheapest/Lowest Rate
    Packers And Movers Mumbai

    ReplyDelete
  7. Packers and Movers Hyderabad Give Certified and Verified Service Providers, Cheap and Best ###Office Relocation Charges, ***Home Shifting, ✔✔✔Goods Insurance worth Rs. 10,000, Assurance for Local and Domestic House Shifting. Safe and Reliable Household Shifting Services in Hyderabad with Reasonable Packers and Movers Price Quotation @ Packers And Movers Hyderabad

    ReplyDelete