Sometimes I need to copy a folder structure from one directory to another. For example, for my business I have directories representing years. Inside those directories I have standard folders for payroll, schedules, accounting, and others. With the new year, I needed to duplicate that folder structure. Any normal person would simply create the half dozen new folders and be done. Being a software developer, I needed to write a program to copy folder structure for me.
Enter the name of the folder that contains the subfolders that you want to duplicate and click List Folders to see the folders that are available. Check the ones that you want to duplicate, enter the path to the folder that should contain the copies, and then click Make Subfolders to copy the folders. This only copies the top-level folders. It does not copy the files within those folders or any subfolders that they may contain.
List Subfolders
When you click List Subfolders, the following code executes.
1 2 3 4 5 6 7 8 9 10 11 12 13 | private void btnListSubfolders_Click(object sender, EventArgs e) { clbSubfolders.Items.Clear(); string from_folder = txtFromFolder.Text; foreach (string subfolder in Directory.GetDirectories(from_folder)) { clbSubfolders.Items.Add(new DirNameInfo(subfolder)); } clbSubfolders.CheckAll(); } |
This code clears the CheckedlistBox
control. It then gets the directory name that you entered and uses the Directory
class’s GetDirectories
method to loop through the folders contained in that directory.
For each subfolder, the code creates a DirNameInfo
object and adds it to the listbox. The following code shows that class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | internal class DirNameInfo { internal string Name; internal string FullName; internal DirNameInfo(string full_name) { DirectoryInfo file_into = new DirectoryInfo(full_name); Name = file_into.Name; FullName = full_name; } public override string ToString() { return Name; } } |
The DirNameInfo
class simply holds a folder’s short and long names. Its overridden ToString
method makes the listbox display the folder’s short name.
After it finishes adding the folder names to the listbox, the btnListSubfolders_Click
event handler uses the following CheckAll
extension method to check all of the listbox’s items.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public static class Extensions { private static void CheckUncheckAll( this CheckedListBox clb, bool is_checked) { for (int i = 0; i < clb.Items.Count; i++) { clb.SetItemChecked(i, is_checked); } } public static void CheckAll(this CheckedListBox clb) { clb.CheckUncheckAll(true); } public static void UncheckAll(this CheckedListBox clb) { clb.CheckUncheckAll(false); } } |
Copy Folder Structure
When you click Make Subfolders, the following code executes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | private void btnMakeSubfolders_Click(object sender, EventArgs e) { string to_folder = txtToFolder.Text; int num_created = 0; foreach (DirNameInfo dir in clbSubfolders.CheckedItems) { if (!CreateSubfolder(to_folder, dir)) break; num_created++; } MessageBox.Show(string.Format("Created {0} folders", num_created)); } |
This code loops through the checked DirNameInfo
objects in the listbox. For each checked object, it calls the following CreateSubfolder
method. If that method returns false to indicate a failure, then the code breaks out of the loop so you can stop early rather than continuing the loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | // Return true if we should continue. private bool CreateSubfolder(string to_folder, DirNameInfo dir) { string path = Path.Combine(to_folder, dir.Name); try { if (Directory.Exists(path)) { if (MessageBox.Show(string.Format( "Subfolder {0} already exists.\n\nContinue?", path), "Folder Exists", MessageBoxButtons.YesNo) == DialogResult.No) return false; } Directory.CreateDirectory(path); Console.WriteLine("Created " + path); } catch (Exception ex) { if (MessageBox.Show(string.Format( "Error creating folder {0}.\n{1}\n\nContinue?", path, ex.Message)) == DialogResult.No) return false; } return true; } |
The CreateSubfolder
method first checks whether the folder already exists. If it does exist, the code tells you and asks if you would like to continue. If you click No, the method returns false so the calling code breaks out of its loop.
If the folder does not already exist, the code uses the Directory
class’s CreateDirectory method to create it.
If there is some other error, the try ... catch
block catches it, displays the exception message, and gives you a chance to exit the subfolder-creation loop.
Summary
I admit that you may not need to copy folder structure often, but if you do, this program may help. The example also demonstrates a couple of useful techniques such as using a class to display strings (short folder names) in a list control, making extension methods to check and uncheck all of a list’s items, and allowing a method to break out of a loop in the calling method.
Download the example to experiment with it and to see additional details.
Source: http://www.csharphelper.com/howtos/howto_copy_folder_structure.html