using System; using System.IO; namespace CopyFilesKeepPathStructure { class Program { static void Main(string[] args) { Console.WriteLine("Root source path"); string rootSourcePath = Console.ReadLine(); //string rootSourcePath = @" c:\folder"; Console.WriteLine("Root destination path"); string rootDestinationPath = Console.ReadLine(); //string rootDestinationPath = @"c:\copyFolder"; Console.WriteLine($"File with list of files to be copied:"); string fileWithListOfFilesToBeCopied = Console.ReadLine(); if (!(fileWithListOfFilesToBeCopied is null) && !(rootSourcePath is null)) { string[] listOfSourceFiles = File.ReadAllLines(fileWithListOfFilesToBeCopied); //string[] listOfSourceFiles = File.ReadAllLines(@"C:\files.txt"); foreach (string sourceFile in listOfSourceFiles) { string folderStructure = sourceFile.Substring(rootSourcePath.Length, sourceFile.Length - rootSourcePath.Length); string destinationFile = rootDestinationPath + folderStructure; Directory.CreateDirectory(Path.GetDirectoryName(destinationFile) ?? string.Empty); File.Copy(sourceFile, destinationFile, true); Console.WriteLine($"Source: {sourceFile}, destination: {destinationFile}"); } } Console.WriteLine($"Press any key..."); Console.ReadKey(); } } }Download from here.
Copy files but keep path structure
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 1446
Here is one my tool to copy files which are listed in a file, and copy them to a location but keep original path tree. Usefull for TortoiseSVN usually I do it when I need to create the patch but I also want to be sure that if patch doesn't work that I have at least some kind of backup.
For example, you want to save files from c:\folder\subfolderOne\file1.txt and c:\folder\subfolderTwo\file1.txt to c:\copyFolder and to keep structure like c:\copyFolder\subfolderOne\file1.txt and c:\copyFolder\subfolderTwo\file1.txt
Here is the code: