Windows command-line regular expression renaming tool: RenameRegex
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!
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”:
Thanks Phil, great catch! Fixed the post.
Is there any chance to make it go into folders recursively? Thanks
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.
For the fist case, windows rename command can do the job.
See rename file extensions
For all other cases, this tool would help.
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
@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);
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!
Thanks Much! Great simple batch rename utility, helps a lot as GUIs couldnt rename/replace extensions that needed to be truncated
Hi,
nice tool. I like it.
btw. the code seems to be updated, but the binary unfortunately not. *hint*
Great tool.
But is there a way to add a prefix or a suffix to a file names?
@Matteo: You could use a regex like:
RR.exe * "(.*)" "prefix$1"
RR.exe * "(.*)" "$1suffix"
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.
Try this instead:
RR.exe * "^(.*)$" "prefix$1"
RR.exe * "^(.*)$" "$1suffix"
It works perfectly! Great!
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?
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#
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?
@Jim – Feel free to open an Issue on the Github project page or a Pull Request if you’re inclined!
https://github.com/nicjansma/rename-regex/
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
Hi Peter! I hope to have some time soon to add that option (and others). I’ve filed some Github issues to track this: https://github.com/nicjansma/rename-regex/issues
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
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)
Works! Thanks!
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
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
@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.
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
The file match is based on the regex, we’re not relying on the OS matching at all.
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
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?
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!
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?
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).