How to rename batches of files

There is nothing as soul-destroying as tediously changing the names of tens or hundreds of files in the Finder. Yet there are times when you need to rename batches of files, perhaps because they came from an old archive, or need to be loaded onto a server that requires different conventions. Rather than waste hours doing it by hand, it repays the modest effort to build an Automator workflow, or the cost of a dedicated tool such as A Better Finder Rename. Used carefully they can whistle through such tasks in the twinkling of an eye.

The most powerful tools for renaming files are, inevitably, the most complex: script languages such as Perl run from Terminal’s command line, using regular expressions (regex). The time and effort required to learn these is great, so unless you can use them in other situations they are unlikely to prove a wise investment. Similarly AppleScript is an excellent language for performing this type of task, but it is Automator’s friendly visual scripting system that offers most users the best power-to-complexity ratio.

Depending on the precise nature of the renaming desired, you might want to remove or replace characters within file names, change the naming convention to incorporate date, sequence numbers, or the like, truncate names, or add or modify extensions. These could be applied to every file, or could depend on existing information such as extensions or file types. The methods offered here should give you a good choice to accomplish every task efficiently.

Finder (Yosemite or later)

frenamefinder11. Use the contextual menu command. Select all the files that you want to rename, and right-click (or two-finger tap) to bring up the Finder’s contextual menu. In that, select the Rename items… command. This gives you three options for renaming, through its popup menu.

frenamefinder22. Replace text. This replaces the text entered into the Find box with that given in the Replace with box. This is ideal for changing file1.txt, for example, to doc1.txt. Note that this cannot change the extension, only the name before the extension.

frenamefinder33. Add text. This adds a fixed string of text either before or after the existing file names, but always before the extension. Thus it would be good for changing file1.txt to bookfile1.txt or file1app.txt.

frenamefinder44. Format. This gives a range of options for serialising the names, which can consist of a custom text element, an index or serial number or date, and the existing name. So you might find it ideal for turning bookchapa.txt into 1Filebookchapa.txt, and so on. You can of course apply these renaming rules in sequence, to perform more complex patterns, such as turning bookchapa.txt into 1LordOfTheRings.txt, perhaps.

NameChanger (donationware)

frenamenamech11. Select files. You can drag and drop files onto the Original Filename pane, or click on the + Add tool to add them using the standard file browser.

frenamenamech22. Set renaming rule. There are ten different options, including Sequence, for generating names using index or serial numbers, and Regular expression, which allows you to use all the power of regular expressions. In each case, the list of Renamed filenames will show you how they will turn out. Once you are happy, click on the Rename tool to rename them. Note that NameChanger treats extensions as regular text elements in the filename, and can therefore be used to change extension if you wish.

Automator

frenameauto11. Create a prompting workflow. Open Automator from your main Applications folder, create a new document in the opening file selector, and select its default option to create a new workflow. The first step in this workflow should be to display a file or folder selection dialog, invoked by the Ask for Finder Items action. Locate this by typing “ask” in the find box, select the Ask for Finder Items action in the resulting list, drag it to the workflow area and drop it there.

frenameauto22. Get chosen folder contents. Set the Type popup menu to accept folders, checking the Allow Multiple Selections box. Most will want to start selection from the Documents folder, although photos, music tracks, and specialist renaming may be better from elsewhere. Enter a meaningful prompt, such as “Choose folders:”, then press Command-S to save your workflow. Clear the contents of the find box, select the Files & Folders item in the Library, and drag and drop the Get Folder Contents action to your workflow.

frenameauto33. Add rename action. In most cases, you will want to rename files contained in sub-folders within the chosen folders, so check the box to repeat for sub-folders. Locate the Rename Finder Items action, drag and drop it to your workflow. You will then see an alert offering to add a Copy Items action: click on the Don’t Add button so that copies are not created. If you inadvertently end up with a Copy Items action, remove it by clicking its close box.

frenameauto44. Select renaming scheme. The Rename Finder Items action contains several quite different renaming schemes, controlled by its uppermost popup menu, each offering quite different settings. For instance, to add a serial number to each image in a sequence, select the Make Sequential item and you can configure in detail how the names will be constructed. You might need to set these to conform to the naming conventions needed to turn a time-lapse photo sequence into a QuickTime movie.

frenameauto55. Add an extension. Another widely useful renaming option is to add an extension to fix the type of file, for instance for folders containing saved email items. This can be achieved using the Add Text item in the top popup menu, choosing “as extension” and specifying the extension to be used, such as “text”. You do not need to preface this with the standard full stop used to separate the extension from the file name, as Automator will handle that automatically.

frenameauto66. Filter file input. One problem with adding an extension in this way is that some file names may already have extensions, which might not be of the type that you wish to set other files; for instance an image might already have .jpg or .jpeg appended to it. To cope with this, drag and drop the Filter Finder Items action into your workflow before the renaming action and set its options to exclude files with known extensions and whose kind is not text.

frenameauto77. Test workflow. Once it is complete, checked and saved, click on the Run button at the top of the window to test your workflow on a test folder. Put a range of test files into that folder that are copies (not originals) and will check that the workflow handles all the tasks that you expect it to. Use the information written to the bottom pane to locate errors. When finished, use the File > Convert to… menu command, select Application, and save to your Utilities folder.

frenameauto88. Make a folder action (optional). Instead of creating an application, you might find it better to implement this as a folder action. Check you have Folder Actions enabled: in the Finder, Alt-click on a folder, and enable in Services > Folder Actions Setup…. Create a new file with Command-N, as a Folder Action not Workflow. Create a working folder and associate the action in the top popup menu. Copy and paste the Filter Finder Items and Rename Finder Items to that and save with Command-S.

A Better Finder Rename (Better Rename in the App Store £14.99)

frenamebetter11. Add the first action. Open Better Rename and drag and drop the folder of files to be renamed into its window, to add them to its action list. Click on the Show Multi-Step Drawer to open the drawer that helps you construct sequential renaming tasks. For the first task, select the Characters category, and in that Remove specific characters. In the list of characters to remove, add ‘ile’, for example.

frenamebetter22. Add further actions. Add a further renaming step by clicking on the + tool at the foot of the drawer. Change this second step to Sequence Numbers, adding a sequence number at the beginning, for example. Once you have built the right sequence of steps, check that the names given in the Change to column at the far right are correct. Then click on the Perform Renames button to commit the proposed changes. You can save any given steps as a preset, using the Presets menu.

Terminal and Regular Expressions using Larry Wall’s Filename Fixer in Perl

frenameperl1. Create Perl script. One shortcoming in OS X is that it lacks a built-in rename command. The standard command for changing file names is mv, whose primary purpose is to move files. You will find it much better to install your own Perl script based on Larry Wall’s Filename Fixer, shown here ready to save to a convenient location. Ensure that you make the file executable, for example editing its permissions using MacPilot or a similar tool.

2. Run commands in Terminal. Your new rename command takes a similar input to the Linux rename, that is a regular expression pattern string and the file(s) on which to apply that renaming. If you want to stick to mv, you will have to write at least a single-liner script to parse out individual name changes, as mv cannot itself handle regular expressions in its input.

Based on a Masterclass which was originally published in MacUser volume 26 issue 8, 2010.