I posted this before but had to delete the question because I explained it so horribly, sorry to those who might have read it before. Let me be much more clear. I have to create a winform application that allows one to copy files from one directory to another by specifying three things:
- Where to copy from
(txtPath) - What file extensions to copy (the ones you specify are the ones that are copied over)
(txtExtensions) - Where to copy to
(txtArchiveTo)
Visually it looks like this:

The copy from the txtPath to the txtArchiveTo has to match how it is copied. That is if txtPath looks like the following:
c:\temp
|
-drivers.exe (file)
-log.txt (file)
-\Test1 (directory)
-\Test2 (directory)
Then if I select for txtArchiveTo a folder such as c:\backup the result should be after my program runs it should look like this:
c:\backup
|
-\temp
|
-drivers.exe (file)
-log.txt (file)
-\Test1 (directory)
-\Test2 (directory)
That is whatever is copied needs to follow the same structure…but I am having a hard time coding this because it looks as though I keep referencing root.Parent.Parent, etc. Let me post some code:
The Go button is very basic:
private void btnGo_Click(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo(txtPath.Text);
WalkDirectoryTree(di);
}
This simply creates a directoryinfo object so I have a handle to the txtPath directory and calls a function WalkDirectoryTree. That function looks like this:
private void WalkDirectoryTree(DirectoryInfo root)
{
System.IO.FileInfo[] files = null;
System.IO.DirectoryInfo[] subDirs = null;
string[] extArray = txtExtensions.Text.Split(' '); //to hold the txtExtensions as an array string { "txt" "tar" "zip" etc.. }
string ext; //to hold the extension
// First, process all the files directly under this folder
try
{
files = root.GetFiles("*.*");
}
catch (UnauthorizedAccessException e)
{
throw; //todo: log this later on...
}
catch (System.IO.DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
}
if (files != null)
{
foreach (System.IO.FileInfo fi in files)
{
string extension = fi.Extension.ToString();
if (extension.IndexOf(".") > -1)
ext = extension.Substring(1, extension.Length-1);
else
ext = extension;
if (extArray.Any(s => ext.Contains(s)))
{
//copy the file
if (Directory.Exists(txtArchiveTo.Text + "\\" + fi.Directory.Name))
{
//directory exists copy the files
}
else
{
Directory.CreateDirectory(txtArchiveTo.Text + "\\" + fi.Directory.Name);
}
File.Copy(fi.FullName, txtArchiveTo.Text + "\\" + fi.Directory.Name + "\\" + fi.Name);
//create a shortcut pointing back to the file...
using (ShellLink shortcut = new ShellLink())
{
shortcut.Target = fi.FullName;
//shortcut.Description = "MY SHORTCUT";
shortcut.DisplayMode = ShellLink.LinkDisplayMode.edmNormal;
shortcut.ShortCutFile = fi.DirectoryName + "\\" + fi.Name + ".lnk";
shortcut.Save();
}
}
}
// Now find all the subdirectories under this directory.
subDirs = root.GetDirectories();
foreach (System.IO.DirectoryInfo dirInfo in subDirs)
{
// Resursive call for each subdirectory.
WalkDirectoryTree(dirInfo);
}
}
}
Please do not pay much attention to the code where I create a shortcut, etc. That part is not the problem. What I am having difficulty with is when I hit these lines of code:
if (extArray.Any(s => ext.Contains(s)))
{
//copy the file
if (Directory.Exists(txtArchiveTo.Text + "\\" + fi.Directory.Name))
{
//directory exists copy the files
}
else
{
Directory.CreateDirectory(txtArchiveTo.Text + "\\" + fi.Directory.Name);
}
The if simply checks to see if the extension of the file matches an extension the user has typed inside of txtExtensions this ensures we are only copying files with those extensions BUT within the folders they are found. My issue is right after that if…I cannot just say:
Directory.CreateDirectory(txtArchiveTo.Text + "\\" + fi.Directory.Name);
The reason being is the archive folder must match the same folder path as what is being copied. So for instance if someone selected c:\temp to search and find txt files to copy from and they selected a folder c:\backup to copy to and c:\temp had 3 subfolders (One, Two, and Three) that also contained text files. The result would be that after my program runs (the completion of "Go") the result would be that the archive folder c:\backup would contain folders (One, Two, and Three) with the txt files inside of them, in addition, c:\backup\One\mytest.txt, etc.
I want to incorporate this in my current code, and feel I am really close but think I need to come up with some recurrision to get the right directory structure. Please do not post me to links about MSDN on how to use Directory.CreateDirectory, or the FileInfo class, I have read these and understand them.
Pass the destination folder in as a paramter to your walking function:
Then when you copy the file, you can just use the CopyTo method of the fileInfo:
When calling the next method recursively, you can just add to the desintation path:
When making the initial call, you will need to compute the output directory: