Click here to Skip to main content
13,554,833 members
Click here to Skip to main content
Add your own
alternative version


61 bookmarked
Posted 10 Feb 2007

Regular Expression Filename Find and Replace

, 10 Feb 2007
Rate this:
Please Sign up or sign in to vote.
Perform find and replace on file names with regular expressions.
Sample image


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.

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
            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);


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.


  • 10th February 2007: First public version.


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


About the Author

M Harris
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.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 2 Pin
idealcp3-Dec-14 23:23
memberidealcp3-Dec-14 23:23 
NewsThere's a new version of the RegEx Tester Tool ! Pin
BucanerO_Slacker1-Mar-08 23:33
memberBucanerO_Slacker1-Mar-08 23:33 
GeneralGetDirectory using dlgOpen.ShowDialog() Pin
avnersimon20-Jan-08 0:44
memberavnersimon20-Jan-08 0:44 
GeneralSimilarity with my article Pin
Gast12821-May-07 2:48
memberGast12821-May-07 2:48 
GeneralRe: Similarity with my article Pin
psyphen23-May-07 21:54
memberpsyphen23-May-07 21:54 
GeneralRe: Similarity with my article Pin
S Douglas20-Jul-07 5:19
memberS Douglas20-Jul-07 5:19 
GeneralRename Master Pin
Lbas15-Feb-07 20:06
memberLbas15-Feb-07 20:06 
GeneralWell done Mark Pin
Chris Austin15-Feb-07 8:01
memberChris Austin15-Feb-07 8:01 
GeneralFeature suggestion: Rename/move to different folder Pin
mikellekim14-Feb-07 8:18
membermikellekim14-Feb-07 8:18 
GeneralRe: Feature suggestion: Rename/move to different folder Pin
M Harris14-Feb-07 11:19
memberM Harris14-Feb-07 11:19 
GeneralSee also RenameIt on SourceForge Pin
mikellekim14-Feb-07 8:15
membermikellekim14-Feb-07 8:15 
GeneralRe: See also RenameIt on SourceForge Pin
M Harris14-Feb-07 11:22
memberM Harris14-Feb-07 11:22 
General32 bit version Pin
Daniel Kamisnki12-Feb-07 8:41
memberDaniel Kamisnki12-Feb-07 8:41 
GeneralRe: 32 bit version Pin
M Harris12-Feb-07 11:22
memberM Harris12-Feb-07 11:22 
GeneralRe: 32 bit version Pin
Daniel Kamisnki12-Feb-07 12:46
memberDaniel Kamisnki12-Feb-07 12:46 
GeneralRe: 32 bit version Pin
M Harris12-Feb-07 12:52
memberM Harris12-Feb-07 12:52 
GeneralWell done. Pin
Palewhale.11-Feb-07 13:10
memberPalewhale.11-Feb-07 13:10 
QuestionErr..but why? Pin
sharpiesharpie10-Feb-07 8:44
membersharpiesharpie10-Feb-07 8:44 
AnswerRe: Err..but why? Pin
M Harris10-Feb-07 14:07
memberM Harris10-Feb-07 14:07 
GeneralRe: Err..but why? Pin
sharpiesharpie11-Feb-07 4:15
membersharpiesharpie11-Feb-07 4:15 
GeneralRe: Err..but why? Pin
M Harris11-Feb-07 11:59
memberM Harris11-Feb-07 11:59 
GeneralRe: Err..but why? Pin
Derek Read12-Feb-07 13:37
memberDerek 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180515.1 | Last Updated 10 Feb 2007
Article Copyright 2007 by M Harris
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid