Batch renaming files

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Posts: 350
Joined: Wed Oct 08, 2008 1:58 am UTC
Location: Guarding the Super Missile

Batch renaming files

Postby SpringLoaded12 » Fri Oct 30, 2015 9:48 pm UTC

I've never done anything with batch scripting before, but as my files grow more & more numerous, I'm finding cases where I need to rename a whole bunch of them at once, and I end up doing so manually. Often the names follow a pattern that should in theory be easy to do faster using a batch script or something similar.

If there's already simple software that is capable of this, I'm equally happy to just use that. But if not, it seemed like a good opportunity to learn how to code something of this type.

My needs vary somewhat, but they would follow this kind of pattern, applying to all files within a directory:
    1. Option to keep or to change the file extension of each file
    2. Option to replace the whole filename (minus the extension), or to only edit parts of it in the style of Find-and-Replace functions in text editors
    3. Option to have it rename the files sequentially (e.g. "001.jpg, 002.jpg, 003.jpg"; "aaa.txt, aab.txt, aac.txt")
    4. Option to apply the same renaming criteria to the contents of each subfolder within the directory
This is all on Windows, of course. If there is not already freeware that can do this, would it be better to use a batch script, or some other type of script? What's the best way to get started?
"It's easy to forget what a sin is in the middle of a battlefield." "Opposite over hypotenuse, dipshit."

User avatar
Yes Man
Posts: 2248
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: Batch renaming files

Postby Flumble » Fri Oct 30, 2015 11:29 pm UTC

I occasionally dust off métamorphose to rename a set of files. I like that it supports regular expressions to search for the right substring and that it shows a preview with colliding names and that it allows you to select whole folder trees and rename them based on their location etcetera.

Yes, it is available for free on windows and it supports all the cases in your list (and a lot more). I don't know if there's an even easier/more developed tool, but this one does a good job.

User avatar
Posts: 350
Joined: Wed Oct 08, 2008 1:58 am UTC
Location: Guarding the Super Missile

Re: Batch renaming files

Postby SpringLoaded12 » Sat Oct 31, 2015 12:14 am UTC

This looks like exactly what I'm looking for. I'll definitely give it a shot. Thanks for the tip.
"It's easy to forget what a sin is in the middle of a battlefield." "Opposite over hypotenuse, dipshit."

User avatar
Posts: 237
Joined: Tue May 22, 2012 5:14 pm UTC
Location: Seoul

Re: Batch renaming files

Postby WanderingLinguist » Mon Nov 02, 2015 5:01 am UTC

On Mac, I generally just use the batch rename feature built in to Finder.

But sometimes that feature isn't quite robust enough for the kind of renaming I want to do. And sometimes I need to do renaming on other platforms (Windows, Linux, etc.)

It's kind of hacky, but a good general solution that I've found is to make batch files in a text editor like Sublime Text that allows multiple selection and good regular expression search/replace.

You start with a listing of the directory contents:

Code: Select all


...and end up with a shell script or batch file for renaming the files, which you then run...

Code: Select all

mv One.png blah_one.png
mv Two.png blah_two.png
mv Thee.png blah_thee.png

For me, the specific steps go something like this:
  1. Use the appropriate command ("dir" on windows, "ls" on other platforms) to get a list of files in the directory.
  2. Copy and paste it into Sublime Text
  3. Use column select to put a cursor instance at the beginning of every line in the file
  4. Ctrl+Shift+End or Cmd+Shift+RightArrow to select the filename, End, Space, Ctrl+V (or Cmd+V) to paste a second copy of the filename (at this point I have two copies of every filename on each line, separated by a space)
  5. Use batch editor or regexp search/replace to do whatever I need to do to the 2nd name on each line
  6. Use the home key and insert the appropriate rename command for each platform at the beginning of each line (again, with multiple cursor instances, this is a single step)
  7. Save with the appropriate shell script / batch file extension
  8. Run it

If the whole multiple cursor instances thing doesn't make sense, take a look at the sublime text web site; the main page has an animation showing multiple cursors. Sublime Text isn't the only editor with that kind of feature; it's just the one I happen to use.

Posts: 207
Joined: Thu May 19, 2011 4:28 pm UTC
Location: Waterloo, Ontario

Re: Batch renaming files

Postby DaveInsurgent » Tue Nov 03, 2015 6:55 pm UTC

A lot of what you describe is just getting good at bash, plus using something like GOW on Windows.

Posts: 194
Joined: Tue Sep 25, 2007 9:10 pm UTC
Location: Belgrade, Serbia

Re: Batch renaming files

Postby bittyx » Wed Nov 04, 2015 6:22 am UTC


I use Sublime Text in combination with massren for doing the same thing. It doesn't solve all problems - in particular, generating new filenames isn't easy (e.g. 00000001.txt, 00000002.txt, etc.) but for renaming it's really awesome (you basically have all of Sublime Text's processing power at your disposal).

On Windows, I used to use Total Commander and its multi-rename tool, which was really awesome - it allows for generating filenames of an arbitrary format, regular expressions, and even importing a list of filenames from a text file. However, I haven't used Windows in a while now, so massren is now my go-to tool for these things.

User avatar
Posts: 360
Joined: Wed Jul 01, 2009 6:25 am UTC

Re: Batch renaming files

Postby roflwaffle » Fri Nov 20, 2015 12:48 am UTC

I use Batch/PS/VB for win specific things that are easier and/or only possible with those languages, and Python for everything else because the syntax is usually easier on the eyes. I'll also use a spreadsheet for simple patterns and/or notepad++ for horizontal text editing if what I'm doing is super simple.

Posts: 472
Joined: Wed Jul 27, 2011 3:13 pm UTC

Re: Batch renaming files

Postby Tub » Sun Nov 22, 2015 2:23 pm UTC

As a linux/KDE user, let me add KRename to the list of GUIs. Or bash for simple regexp-replacements, when I feel like it.

Something like this should list all png files and echo a rename command for each, in this case substituting .png for .jpg

Code: Select all

for i in *.png ; do echo mv -i \"$i\" \"$(echo $i | sed 's/\.png/.jpg/g')\" ; done

Take a look at the resulting list, then re-run the command while piping the output to bash, done.

Return to “Coding”

Who is online

Users browsing this forum: ucim and 8 guests