Wednesday 10 January 2018

Search all diectiores and copy a given file name to other location of any format

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        public static void Main()
        {
            string sDir = @"D:\Find\";
            DirSearch(sDir);
        }


        public static void DirSearch(string sDir)
        {
            try
            {
                //Source Path
                sDir = @"D:\Find\";

                //Destination Path
                string  dDir = @"D:\\";

                //Files to search
                string[] copyfiles = new string[] { "VisualStudioKey1.zip", "VisualStudioKey.zip" };

                //Get all folders in given location
                string[] allfiles = Directory.GetDirectories(sDir;

                //Loop through each folder to find files
                foreach (string d in allfiles)
                {
                    //Loop through each file and copy to destination
                    foreach (var filestToCopy in copyfiles)
                    {
                        //Getting the specified file in the location
                        foreach (string f in Directory.GetFiles(d + "\\", filestToCopy))
                        {
                            Console.WriteLine(f + "\n");
                            System.IO.File.Copy(f, dDir + filestToCopy, true);
                        }
                    }

                }
                Console.ReadLine();
            }
            catch (System.Exception excpt)
            {
                Console.WriteLine(excpt.Message);
            }
        }
    }
}

No comments:

Post a Comment