How to recursively list all the files in a directory in C# and copy all files to another directory
I am trying to write a c# console application that recursively reads through a certain folder. In these folders are thousands of .jpg images
The folder structure is very deep in some levels and an example look like this:
Scc-LocalPhoto/testfiles/1997/JAN-JUN 1997/APRIL 1997/7.4.97 - 11.4.97/FRI11.4.97/
As you can see the folder structure is quite messy, however I do not have control over this.
My task is to read through all the folders. Retract the Meta data from the images and store in XML file. I then need to copy all the folders in the same layout and paste them in a new folder.
I think I will be able to read though all the directories and extract the meta data from the images and save it to an xml file.
What I do not know how to do is copy and past all the folders and images and paste them in a new directory maintaining the same folder structure.
Does anybody know of an efficient way to perform this task or is there any project, code available I could use as a starting point.
I am fairly new to C# and writing console app开发者_JAVA百科s. Thanks for your time.
Parsing Directories Recursively
static void ParseDirectories(string root)
{
ProcessDirectory(new DirectoryInfo(root));
string[] subDirectories = Directory.GetDirectories(root);
// No more directories to explore
if (subDirectories.Length == 0)
return;
foreach (string subDirectory in subDirectories)
{
ParseDirectories(subDirectory);
}
}
Processing the Files in a Directory
static void ProcessDirectory(DirectoryInfo directory)
{
foreach (FileInfo file in directory.EnumerateFiles("*.jpg")
{
// record metadata and do other work on each file here
}
}
Copying a Directory tree
static void CopyDirectoryTree(DirectoryInfo source, DirectoryInfo dest)
{
if (!Directory.Exists(dest.FullName))
Directory.CreateDirectory(dest.FullName);
bool overwrite = true;
// Copy files
foreach (FileInfo file in source.EnumerateFiles())
{
file.CopyTo(Path.Combine(dest.ToString(), file.Name), overwrite);
}
// Copy Sub-directories
foreach (DirectoryInfo subDirectory in source.GetDirectories())
{
DirectoryInfo newDirectory = destination.CreateSubdirectory(subDirectory.Name);
CopyDirectoryTree(subDirectory, newDirectory);
}
}
Sample usage
static void Main(string[] args)
{
// Process each directory
string initialDirectory = @"C:\path_to_folder";
ParseDirectories(initialDirectory);
// Copy directory tree
string destinationDirectory = @"C:\path_to_new_root_directory";
CopyDirectoryTree(
new DirectoryInfo(initialDirectory),
new DirectoryInfo(destinationDirectory));
}
Hope this helps!
May I suggest the following, which is, in my opinion, a little bit more straightforward
public static void CopyFolderTree(string sourcePath, string targetPath)
{
var sourceDir = new DirectoryInfo(sourcePath);
var targetDir = new DirectoryInfo(targetPath);
targetDir.Create();
foreach(var file in sourceDir.GetFiles())
file.CopyTo(Path.Combine(targetPath, file.Name), true);
foreach(var subfolder in sourceDir.GetDirectories())
CopyFolderTree(subfolder.FullName, Path.Combine(targetPath, subfolder.Name));
}
精彩评论