This example shows how to use regular expressions to rename files. To use the program, enter a directory name and a file pattern. Also enter regular expressions for the find and replace patterns. When you click Preview Changes, the program searches the directory for files matching the file pattern. For those files, it makes replacements using the regular expressions. If the result is different from the original file’s name, it lists the file’s original and new file names in a ListView control.
Review the list to ensure that it makes sense. Click on a row and click the remove button to remove that file from the list.
When you’re satisfied, click the Make Changes button to rename the files.
Be sure to check the list carefully. Don’t click the Make Changes button if any file names will cause conflicts. For example, don’t give a file a new name that is the same as that of an existing file. (Unless the existing file will be renamed first. You can see that this could get complicated so I didn’t try to figure out whether there will be a problem.)
The MakeFileLists method shown in the following code does the most interesting work.
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 30 31 32 33 34 35 36 37 38 39 | // Make a list of file names to change from and to. private List<string> FullFromNames, FromNames, ToNames; private void MakeFileLists() { try { // Make the file name lists. FullFromNames = new List<string>(); FromNames = new List<string>(); ToNames = new List<string>(); // Get the files that match the pattern. DirectoryInfo dir_info = new DirectoryInfo(txtDirectory.Text); FileInfo[] files = dir_info.GetFiles(txtFilePattern.Text); Regex regex = new Regex(txtOldPattern.Text); for (int i = 0; i < files.Length; i++) { string new_name = regex.Replace(files[i].Name, txtNewPattern.Text); new_name = new_name.Replace("$i", i.ToString()); if (files[i].Name != new_name) { FullFromNames.Add(files[i].FullName); FromNames.Add(files[i].Name); ToNames.Add(new_name); } } } catch (Exception ex) { MessageBox.Show("Error building file list.\n" + ex.Message); FullFromNames = new List<string>(); FromNames = new List<string>(); ToNames = new List<string>(); } } |
This method creates lists to hold the names of the files. It uses a DirectoryInfo
object’s GetFiles
method to get a list of the files that match the file pattern. You can change that call to recursively search sub-directories if you like.
The code then loops through those files. For each file, it uses a Regex
object to make the regular expression replacements. Regular
expressions are quite complex so they’re not described here. Perhaps I’ll cover them later. Until then, see Microsoft’s article .NET Framework Regular Expressions.
Next, the code replaces $i with the current file number. That makes it easy to number the files in a directory.
Finally, if the new file name is not the same as the original name, the program adds the file to the lists.
Download the example to experiment with it and to see additional details.
http://www.csharphelper.com/
[…] to rename files that match a pattern in C# to let you rename files within a date range. See that example for most of the details. This post describes the changes from that […]