Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Regular Expression Filename Find and Replace

4.86/5 (29 votes)
10 Feb 20072 min read 3   2.3K  
Perform find and replace on file names with regular expressions.
Sample image

Introduction

The purpose of this application is to allow you to find and replace files names using regular expressions. At first you may find this would be a pretty strange thing to do. Consider you have a directory full of files that follow two different naming conventions – both contain the same information but just display it differently. If you want to standardize these file names, you would have to edit each filename separately. Renaming several thousand files is not my idea of fun, so I wrote this program to find and replace across file names.

Using the Program

Once you open the program, you can select the directory to find and replace in. Underneath this is the option to find in subdirectories as well.

The find and replace sections allow you to enter a regular expression which you want to find and replace. Standard regular expression syntax applies here. If you are looking for a tool in which to test or design regular expressions, I highly recommend Expresso here on Code Project.

In the Find box you also get a set of options for the Regular Expression, if nothing else I recommend that you keep Compiled turned on as it does make quite a difference to the speed.

Before running the regular expression over your file names, I suggest that you run the test first. This will do everything the replace will except the file rename. Using this allows you to see the results and make sure it is working correctly. A file rename cannot be undone by this program; I'd hate to be responsible for changing 2000 file names into meaningless garbage!

Testing the regular expression.
Testing the regular expression

Under the file menu you will find the ability to save and load "projects". This just saves the values of all the text and check box fields on the form to xml which you can upload at a later date. The idea is that it saves you having to remember or re-write a very complex regular expression in the future.

How it Works

The code for this program is incredibly simple, there is nothing terribly complex at all about it. All actual processing of file names is done in frmReplace. The ReplaceFiles method does all the work, it gets the list of files and then iterates over each of them checking for a Regex match. If there is a match it writes it to the list control as a new item, and then renames the file.

C#
foreach (string file in files)
{
    // move the progress bar along
    pbReplace.Value = ++i;

    // find just the file name.. no need to replace anything in the path
    string name = Path.GetFileName(file);

    // does it match the expression?
    if (rex.IsMatch(name))
    {
        // find out the new name after replacement
        string newName = rex.Replace(name, replace);

        // the path that this came from
        string path = Path.GetDirectoryName(file);

        // add an item to the list showing the replacement
        lstReplacements.Items.Add(
            path + Path.DirectorySeparatorChar + name + " -> " + 
            path + Path.DirectorySeparatorChar + newName);

        // don't rename the file if this is only a test run
        if (!testOnly)
        {
            // rename the file
            File.Move(path + Path.DirectorySeparatorChar + name, 
                path + Path.DirectorySeparatorChar + newName);
        }
    }
}

Conclusion

This has been a very short article, there isn't really much more that can be covered on a program that only has 60 or so lines of code that does anything exciting! I hope you find the program useful.

History

  • 10th February 2007: First public version.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here