[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Merge directories in C#

[Merge directories in C#]

This example lets you merge directories.

Once upon a time, if you used Windows Explorer to move a file from one directory to another and the destination directory already contained a file with that name, you had the option of keeping both versions of the file. For example, if the file as called Readme.txt, you could give the moved file the name Readme (2).txt. (Or am I imagining all that?)

Now File Explorer (which is basically Windows Explorer with a new name) doesn't give me that option. It only lets me decide whether to overwrite the existing file or to cancel the move.

This program merges directories by moving the files from one directory into another. If a file's name is already used in the destination directory, the program appends numbers to the end of the name until it finds a name that isn't already used. It tries names such as Readme.txt, Readme (2).txt, Readme (3).txt, and so forth until it finds an available name.

Enter the From and To directories. If you click the ellipsis button to the right, the program displays the (admittedly pathetic) folder browser dialog so you can browse to select the directories.

When you click the Merge button, the program executes the following code.

// Move files in the From directory into the To directory. private void btnMerge_Click(object sender, EventArgs e) { // Get the From directory.. string from_dir = txtFrom.Text; if (from_dir.Length == 0) { MessageBox.Show("Please enter a From directory", "Missing Directory", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (!Directory.Exists(from_dir)) { MessageBox.Show("From directory " + from_dir + " doesn't exist", "No Such Directory", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // Get the To directory.. string to_dir = txtTo.Text; if (to_dir.Length == 0) { MessageBox.Show("Please enter a To directory", "Missing Directory", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (!Directory.Exists(to_dir)) { MessageBox.Show("To directory " + to_dir + " doesn't exist", "No Such Directory", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // Make sure the directories are different. if (from_dir.ToLower() == to_dir.ToLower()) { MessageBox.Show( "The From and To directories must be different", "Duplicate Directories", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // Make sure the directory names end with \. if (!from_dir.EndsWith("\\")) from_dir += "\\"; if (!to_dir.EndsWith("\\")) to_dir += "\\"; // Move the files. Cursor = Cursors.WaitCursor; Refresh(); int num_moved = 0; int num_renamed = 0; foreach (string old_name in Directory.GetFiles(from_dir)) { // Compose the new file name. FileInfo info = new FileInfo(old_name); string new_name = to_dir + info.Name; // As long as the file already exists, // append an increasing number. if (File.Exists(new_name)) { string extension = info.Extension; string short_name = info.Name.Substring(0, info.Name.Length - extension.Length); for (int i = 1; File.Exists(new_name); i++) { new_name = to_dir + short_name + " (" + i.ToString() + ")" + extension; } num_renamed++; } // Move the file. File.Move(old_name, new_name); num_moved++; } // Display results. Cursor = Cursors.Default; MessageBox.Show("Moved " + num_moved + " files\nRenamed " + num_renamed + " files"); }

First, the code gets and validates the From and To directory names. If either directory is blank or doesn't exist, or if the directories are the same, the method displays an error message and exits. The code makes sure each directory's name ends with \ and then moves the files.

To move the files, the program loops through the file names returned by the Directory.GetFiles method called for the From directory. For each file, the program composes a new file name in the To directory. If that file already exists, the program enters a loop where it composes a new file name appending (1), (2), (3), and so forth to the original file's name. When it finds a name that isn't already in use, the loop ends and the program moves the file from its old location to its new one.

When it finishes, the program displays the number of files it moved and the number of files it renamed.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.