Windows command-line regular expression renaming tool: RenameRegex

January 30th, 2012

Every once in a while, I need to rename a bunch of files.  Instead of hand-typing all of the new names, sometimes a nice regular expression would get the job done a lot faster.  While there are a couple Windows GUI regular expression file renamers, I enjoy doing as much as I can from the command-line.

Since .NET exposes an easy to use library for regular expressions, I created a small C# command-line app that can rename files via any regular expression.

Usage:

RR.exe file-match search replace [/p]
  /p: pretend (show what will be renamed)

You can use .NET regular expressions for the search and replacement strings, including substitutions (for example, "$1" is the 1st capture group in the search term).

Examples:

Simple rename without a regular expression:

RR.exe * .ext1 .ext2

Renaming with a replacement of all "-" characters to "_":

RR.exe * "-" "_"

Remove all numbers from the file names:

RR.exe * "[0-9]+" ""

Rename files in the pattern of "123_xyz.txt" to "xyz_123.txt":

RR.exe *.txt "([0-9]+)_([a-z]+)" "$2_$1"

Download

You can download RenameRegex (RR.exe) from here.  The full source of RenameRegex is also available at GitHub if you want to fork or modify it. If you make changes, let me know!

Share this:

  1. Phil Quinn
    February 1st, 2012 at 04:44 | #1

    Hi — thanks for this. I noticed a small typo in this line: Rename files in the pattern of “124_xyz.txt” to “xyz_123.txt”:

  2. February 1st, 2012 at 09:16 | #2

    Thanks Phil, great catch! Fixed the post.

  3. Wesley
    February 1st, 2012 at 18:31 | #3

    Is there any chance to make it go into folders recursively? Thanks

  4. February 2nd, 2012 at 10:14 | #4

    It doesn’t currently, but that would be a fairly simple update to the code. Are you comfortable making changes? Feel free to contribute edits at https://github.com/nicjansma/rename-regex, or even just open an issue there as a feature request.

  5. Giri
    June 12th, 2012 at 23:11 | #5

    For the fist case, windows rename command can do the job.
    See rename file extensions

    For all other cases, this tool would help.

  6. CZNeo
    October 8th, 2012 at 11:31 | #6

    Hi,

    I need to rename a couple of files that would contain only the lowercase english chars (ie. ŽČs.jpg -> zcs.jpg)

    Is that possible?

    Thx,

    CZNeo

  7. October 8th, 2012 at 11:46 | #7

    @CZNeo

    The app currently ignores case in the RegEx, but you can change this line 56 in Program.cs:

    // rename via a regex
    string fileNameAfter = Regex.Replace(fileName, nameSearch, nameReplace, RegexOptions.IgnoreCase);

    To remove RegexOptions.IgnoreCase:


    // rename via a regex
    string fileNameAfter = Regex.Replace(fileName, nameSearch, nameReplace);

  8. WCWedin
    February 5th, 2013 at 14:02 | #8

    I’m commenting to say that this little program perfectly met my needs in a minor emergency and got me out of a tight spot! Thanks!

  9. Herm
    March 17th, 2015 at 15:40 | #9

    Thanks Much! Great simple batch rename utility, helps a lot as GUIs couldnt rename/replace extensions that needed to be truncated

  10. ozz
    June 7th, 2015 at 08:12 | #10

    Hi,
    nice tool. I like it.
    btw. the code seems to be updated, but the binary unfortunately not. *hint*

  11. Matteo
    August 11th, 2018 at 02:58 | #11

    Great tool.
    But is there a way to add a prefix or a suffix to a file names?

  12. August 22nd, 2018 at 06:15 | #12

    @Matteo: You could use a regex like:

    RR.exe * "(.*)" "prefix$1"
    RR.exe * "(.*)" "$1suffix"

  13. Matteo
    September 5th, 2018 at 23:28 | #13

    Thank you Nic. But when I use the prefix code I have the prefix added both at the beginning and at the end of the file name.
    FILENAME.ZIP –> prefixFILENAME.ZIPprefix

    The suffix code added the suffix two times
    FILENAME.ZIP –> FILENAME.ZIPsuffixsuffix

    I tried to edit the code, but it always added something at the end.

  14. September 13th, 2018 at 16:46 | #14

    Try this instead:


    RR.exe * "^(.*)$" "prefix$1"
    RR.exe * "^(.*)$" "$1suffix"

  15. Matteo
    September 13th, 2018 at 23:18 | #15

    It works perfectly! Great!

  16. computerprep
    April 18th, 2019 at 06:39 | #16

    This could fit my need perfectly!!! I’m just struggling to implement in my test folder before rolling out.

    I have a filename pattern like .*set.*[0-9]img.*[0-9].*
    To explain that in plain english (if my regex is off) the filenames start with any string of unknown characters (including periods, hyphens, commas, apostrophes, etc) followed by a specific pattern set#img# (where the number is between 1-4 digits long), followed by another string of unknown characters.

    I need to remove all characters after the set#img# pattern in the middle. Any advice?

    • April 30th, 2019 at 17:49 | #17

      You could use something like this maybe, with a replacement pattern?

      rr * “(.*set[0-9]+img[0-9]+).*” “$1” /p

      That would remove everything after set#img#

  17. September 26th, 2019 at 16:01 | #18

    I love this tool! Quick and easy, the only thing lacking for me is the ability to also rename *folders* – is that something that could be added?

  18. Peter
    January 7th, 2020 at 06:22 | #20

    As I read in comments that RR does not pay attention to Upper vs. Lower case.
    I need this option to replace e.g. ” rel. ” by ” Rel. ” in filenames.

    You have the recommendation to change this line 56 in Program.cs:

    Hmm, I am not a programmer. If it that easy: could you provide a changed RR version WITH upper/lowercase attention?

    Thank you
    Peter

  19. Matteo
    January 30th, 2020 at 04:39 | #22

    Hi Nic, I need your help again if you can.
    I’d like to use your awesome tool in a net like “\\NAS\folder\” but windows command line doesn’t accept UNC directories. I tried to use Power shell but it doesn’t work properly.

    I need to rename file like in your example: “123_xyz.txt” to “xyz_123.txt”.

    .\RR.exe *.txt "([0-9]+)_([a-z]+)" "$2_$1" showed the help, so I modified in this:
    .\RR.exe *.txt "(?[0-9]+)_(?[a-z]+)" "${1}_${2}" but the result is the following:
    124_xyz.txt -> _.txt (pretend)

    Where am I wrong?
    Thank you very much

  20. February 14th, 2020 at 05:17 | #23

    Hi @Matteo!

    For UNC paths, can you “pushd” to them first so the folder is mapped as a local letter?

    I’ve just verified that this command should work:

    RR *.txt "([0-9]+)_([a-z]+)" "$2_$1" /p
    123_xyz.txt -> xyz_123.txt (pretend)

  21. Matteo
    February 17th, 2020 at 12:57 | #24

    Works! Thanks!

  22. Tom
    April 25th, 2020 at 09:20 | #25

    I _LOVE_ this tool and I use it all of the time. It saves me so much time and effort in file management.

    Thank you VERY much!
    Tom

  23. huynh hieu
    May 11th, 2020 at 05:34 | #26

    Hi Nic!
    I need to change filenames, such as “”somethingtest01”, “somethingtest02”, …. to “somethingtest02”, “somethingtest03” (increment by 1 or an other number step)
    Could you help me

    • May 11th, 2020 at 06:57 | #27

      @huynh: I don’t think rename-regex is needed for that, but you should be able to do a loop in a batch file starting with the highest number, moving it forward by one, decrementing the number by one, moving that forward by one, etc.

  24. Carlos
    January 12th, 2021 at 08:59 | #28

    Nic, This tool is quite useful !!
    I’ve one question, the “file match” part is also RegEx based or is just standard wildcard processing from operating system?
    Kind regards

    • January 12th, 2021 at 10:19 | #29

      The file match is based on the regex, we’re not relying on the OS matching at all.

  25. justin
    December 3rd, 2021 at 13:15 | #30

    Just want to say I absolutely love this binary and thank you. I’ve written my own variants over the years; in bash, perl, python, and even cmd. But this is the easiest to just use without having to constantly remind myself of the parameters, argument order, direction of masking slashes etc.
    P.s. I’ve always had a soft spot for this jscript polyglot cmd implementation of a regexp renamer also: https://github.com/Thdub/Optimize_NextGen/blob/master/Files/Utilities/JREPL.bat

  26. December 6th, 2021 at 01:03 | #31

    To rename:
    “file .ext” to “file.ext”
    rr *.txt ” .txt” “.txt”
    it works

    To rename:
    “file..ext” to “file.ext”
    rr *.txt “..txt” “.txt”
    It doesn’t work, I rename all the files.
    What am I wrong?

    • February 1st, 2022 at 16:54 | #32

      One thing to remember is that the search and replacement patterns are regular expressions, and a “period” (.) in a regular expression matches any character.

      So this command:

      rr *.txt "..txt" "2.txt"

      Really means “replace [anything][anything]txt” with “.txt”.

      Instead, you’ll want to escape the period so it’s taken literally:

      rr *.txt "\.\.txt" ".txt"

      Then it should just replace files with “..txt” in the name.

      My test examples:

      RR.exe *.txt " .txt" ".txt"
      name with space .txt -> name with space.txt

      RR.exe *.txt "\..txt" ".txt"
      name with double period at end..txt -> name with double period at end.txt

      Hope that helps!

  27. Pendo Seven
    August 12th, 2023 at 01:07 | #33

    Hi, I love this little app, and use it often, but have found an annoying situation I hope you have a solution for.

    When using the command: k:\apps\rr.exe * “(\D+)(\d)\.jpg” “$1000$2.jpg”

    To pad the filename with numbers to 4 digits with zeros, I’ve found that this works ok except when the placing the $1 at the start of any replacement filename.

    I’ve tried many possible combinations of placing “^” and “$” and in the search filename text with no joy.

    The below shows an example:

    pic_1.jpg -> $10001.jpg
    pic_2.jpg -> $10002.jpg
    pic_3.jpg -> $10003.jpg
    pic_4.jpg -> $10004.jpg
    pic_5.jpg -> $10005.jpg
    pic_6.jpg -> $10006.jpg
    pic_7.jpg -> $10007.jpg
    pic_8.jpg -> $10008.jpg
    pic_9.jpg -> $10009.jpg

    Is there a way to overcome this apparent bug?

  28. Pendo Seven
    August 12th, 2023 at 13:14 | #34

    Not sure what happened to my comment/question that I posted (as it’s now missing), but I’ve found a solution to it myself so you don’t have to answer unless you want to. FYI, my solution was to include the grouping number is “{” and “}”, e.g. instead of “$1000$2.jpg”, it would instead be “${1}000${2}.jpg” (the second pair isn’t required, but I used it this way to be consistent).

  1. No trackbacks yet.