Copy Files of a Directory in Another Directory Recursively.

Code Snippets 4 U

This function accepts three parameters.

  1. To copy only files with specified extension
  2. To Add Suffix to the file name

It is an example and you can bend it according to your needs.

Note: There is a constants class that have to be created in order to run this example. It contains various constants needed.

using FileManipulation.constants;
using System;
using System.IO;

namespace FileManipulation
{
    internal class SpecificFileCopier
    {
        private static readonly string startDestination = Constants.ModificationSourceFolder;
        private static readonly string startSource = Constants.OriginalLocation;

        /// <summary>
        /// Copies specified extension files to a location
        /// Also supports suffix addition to fileName
        /// </summary>
        /// <param name="extension"></param>
        /// <param name="suffix"></param>

        internal void Copy(string extension, string suffix="")
        {
            string[] files = new string[] { };
            try
            {
                files = Directory.GetFiles(startSource, "*." + extension, SearchOption.AllDirectories);
            }
            catch (UnauthorizedAccessException)
            {
                Console.WriteLine(Constants.Unauthorized);
                return;
            }
            for(int i=0;i<files.Length;i++)
            {
                FileInfo file = new FileInfo(files[i]);
                
                string fileDirectory = file.Directory.ToString();

                // get only the path after startDestination
                string newDirectoryToBeCreated = startDestination + fileDirectory.Remove(0,startSource.Length);
               
                // create parent directories if doesn't exist
                if (!Directory.Exists(newDirectoryToBeCreated))
                {
                    Directory.CreateDirectory(newDirectoryToBeCreated);
                }

                // change file.Name if rename is set to true
                string fileName = file.Name;

                if (suffix != "")
                {
                    string newFileName = fileName.Insert(fileName.LastIndexOf('.'), suffix);
                    try
                    {
                        File.Copy(files[i], Path.Combine(newDirectoryToBeCreated, newFileName));
                    }
                    catch (IOException) { }
                    catch (Exception) { }
                }
                // copy file with original name to specified path
                try
                {
                    File.Copy(files[i], Path.Combine(newDirectoryToBeCreated, fileName));
                }
                catch (IOException) { }
                catch (Exception) { }


            }
            Console.WriteLine(Constants.CopySuccessful);
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

thirteen − = three