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();
        }
    }
}

24 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
  8. I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts.
    Cyber Security Projects for Final Year

    JavaScript Training in Chennai

    Project Centers in Chennai

    JavaScript Training in Chennai

    ReplyDelete
  9. Packers and Movers Ahmedabad - We Provide ***Best Service Providers, Safe, Reliable, Affordable, Trusted ###Movers and Packers in Ahmedabad List, Household Shifting, Office Relocation: Choose Top Verified Packers and Movers Ahmedabad Compare ???Shifting Service Chrages, Price Quotation, Rate List Charts and Save Money and Time @ Packers and Movers Ahmedabad

    ReplyDelete
  10. Superb post, we enjoyed each and everything as per written in your post. Thank you for this informative article because it’s really helpful, I really like site.
    Please visit our website: @ Packers and Movers Kolkata

    ReplyDelete

  11. The sexy women we have with us at our agency atRussian Call Girls in Connaught Place has a unique set of sex talents for their clients. They might even strip your clothes off and give you the best blowjob that you have been longing for! Check our other Services...
    Russian Call Girls in Vasant Kunj
    Russian Escorts in Connaught Place
    Russian Escorts in Connaught Place
    Russian Escorts in Connaught Place
    Russian Escorts in Connaught Place
    Russian Escorts in Connaught Place


    ReplyDelete
  12. Are you looking for escort service in Mumbai? Enjoy with College girls and became your trip amazing with charming Mumbai escorts girls at cheap rate 24/7

    Mumbai Escorts | Call Girls in Mumbai | Mumbai Call Girls | Escorts in Mumbai | Russian Mumbai Escorts | Call Girls Service in Mumbai | Escort Service in Mumbai

    ReplyDelete


  13. The hectic world, a usual silhouette of wrinkled-faces and lack of time & good people make absolute absence of Love in our lives. Only a smile means to exist in the whole world bringing itself a Comet of Love. So it takes you believe onRussian Escorts in Gurgaon liable to fulfill your lives with real sense of love. Check our other Services...
    Escorts Service in Gurgaon
    Female Escorts in Gurgaon
    Russian Escorts in Gurgaon
    Russian Escorts in Gurgaon
    Escorts Service in Gurgaon





    ReplyDelete