Click here to Skip to main content
15,880,608 members
Articles / Programming Languages / C#
Article

Regular Expression Filename Find and Replace

Rate me:
Please Sign up or sign in to vote.
4.86/5 (29 votes)
10 Feb 20072 min read 110.6K   2.3K   61   23
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


Written By
Software Developer
Australia Australia
Mark spends his spare time working on his radio control planes, helicopters and trucks. He devises new ways to make them crash faster and easier than ever before. Mark has progressed this joy of destroying nice toys into build UAV's - which can crash themselves with, or without user input.

Mark enjoys all aspects of C#, specifically windows programming and regular expressions.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 1481735829-Apr-20 7:29
Member 1481735829-Apr-20 7:29 
GeneralMy vote of 2 Pin
idealcp3-Dec-14 23:23
idealcp3-Dec-14 23:23 
NewsThere's a new version of the RegEx Tester Tool ! Pin
Pablo Osés1-Mar-08 23:33
Pablo Osés1-Mar-08 23:33 
GeneralGetDirectory using dlgOpen.ShowDialog() Pin
avnersimon20-Jan-08 0:44
avnersimon20-Jan-08 0:44 
GeneralSimilarity with my article Pin
Gast12821-May-07 2:48
Gast12821-May-07 2:48 
GeneralRe: Similarity with my article Pin
Gavin Kendall23-May-07 21:54
professionalGavin Kendall23-May-07 21:54 
GeneralRe: Similarity with my article Pin
S Douglas20-Jul-07 5:19
professionalS Douglas20-Jul-07 5:19 
GeneralRename Master Pin
Lbas15-Feb-07 20:06
Lbas15-Feb-07 20:06 
GeneralWell done Mark Pin
Chris Austin15-Feb-07 8:01
Chris Austin15-Feb-07 8:01 
GeneralFeature suggestion: Rename/move to different folder Pin
mikellekim14-Feb-07 8:18
mikellekim14-Feb-07 8:18 
One of the things you might want to do when you're renaming those episodes is to change the folder structure in which they're stored. Correct me if I'm wrong, but it seems that's not currently supported.
GeneralRe: Feature suggestion: Rename/move to different folder Pin
M Harris14-Feb-07 11:19
M Harris14-Feb-07 11:19 
GeneralSee also RenameIt on SourceForge Pin
mikellekim14-Feb-07 8:15
mikellekim14-Feb-07 8:15 
GeneralRe: See also RenameIt on SourceForge Pin
M Harris14-Feb-07 11:22
M Harris14-Feb-07 11:22 
General32 bit version Pin
Daniel Kamisnki12-Feb-07 8:41
Daniel Kamisnki12-Feb-07 8:41 
GeneralRe: 32 bit version Pin
M Harris12-Feb-07 11:22
M Harris12-Feb-07 11:22 
GeneralRe: 32 bit version Pin
Daniel Kamisnki12-Feb-07 12:46
Daniel Kamisnki12-Feb-07 12:46 
GeneralRe: 32 bit version Pin
M Harris12-Feb-07 12:52
M Harris12-Feb-07 12:52 
GeneralWell done. Pin
Palewhale.11-Feb-07 13:10
professionalPalewhale.11-Feb-07 13:10 
QuestionErr..but why? Pin
sharpiesharpie10-Feb-07 8:44
sharpiesharpie10-Feb-07 8:44 
AnswerRe: Err..but why? Pin
M Harris10-Feb-07 14:07
M Harris10-Feb-07 14:07 
GeneralRe: Err..but why? Pin
sharpiesharpie11-Feb-07 4:15
sharpiesharpie11-Feb-07 4:15 
GeneralRe: Err..but why? Pin
M Harris11-Feb-07 11:59
M Harris11-Feb-07 11:59 
GeneralRe: Err..but why? Pin
Derek Read12-Feb-07 13:37
Derek Read12-Feb-07 13:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.