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!