Everyone occasionally dives right into a problem without researching (gasp!) the best solution. For me, this once meant manually renaming hundreds of files and moving them into individual folders in preparation for upload to a digital repository. Then finally a colleague said to me, and rightly so, “Are you crazy? There’s scripts to do that for you.”
In my last post, I discussed file naming conventions and the best methods to ensure future access and use for files. However, as librarians and archivists, we don’t always create the files we manage. Donors bring hard drives and students bring USB drives and files get migrated…etc, etc. Renaming existing files to bring them in line with our standards is often a daunting prospect, but there are lots of methods available to save time and sanity.
In this post, I’ll review a few easy methods for batch renaming files:
- Automator for Mac OS X – a built-in tool which aids in building scripts for this type of task
- Column Editor for Notepad++ in Windows – edit a list of rename commands, then run in a batch
- Batch File with a Loop in Windows – create a batch file which loops through the files and executes rename commands
The first two methods do not require any knowledge of coding; the last is slightly more advanced. There are some caveats: if you are an experienced developer, it’s likely that you know a more efficient way. I also tried to avoid any third-party tools specifically touted as renaming applications, as I have not used them and therefore cannot recommend which is best. Lastly, while Photoshop and other photo editing software may help with this when working with image files, the options listed below should work with all file types.
In my example, I am using a set of 43 images waiting for upload to our digital library. The files originated on a faculty member’s camera, so the names are in the following format:
DSCN2956.jpg DSCN2957.jpg DSCN2958.jpg ...
The images are of the Olympic Stadium in Beijing, China, and I would like the file names to reflect that, i.e. Beijing-OlympicStadium-01.jpg
One of the features included in Mac OS X (10.4 and above) is Automator, the “personal automation assistant”, according to Apple Support. The tool allows you to define a group of actions to take, automatically, on a given set of triggers. For example, after discovering this tool I created a script which, when prompted, quickly grabs a screenshot and save it as a jpeg in a folder I specified.
For this post, let’s step through using the tool to batch re-name files. First, I found a tutorial online. These are everywhere, but specifically, I looked at “10 Awesome Uses for Automator Explained” by Matt Reich. Reich gives a good succinct tutorial, placed in the context of personal photos. We’re going to make a few changes in our steps, place it in the context of a digital collection and walk a little more slowly through the process. I’ll be using Mac OS 10.8 in the steps and screenshots.
1. Go to Finder, Open Applications and double-click on Automator.
2. We’re going to create an Application. Reich uses a Folder Action, which means that you would copy the items into the folder which would trigger the rename. That approach makes sense as you move personal photos from a camera into the same Photos folder over and over again (in fact, I plan to use it myself). However, in working with existing digital files that we just want to rename, which may need to live in many different folders, the Application is a more direct approach. This will allow us to act on the files in place. So, click on the Application Icon, and click on Choose.
3. Now we need to add some Actions. In the Library along the far left-hand pane, select “Files & Folders”. The middle pane will now show all of the options for acting on Files & Folders.
4. Click on “Rename Finder Items” and drag it to the large empty pane on the right.
5. The system will prompt you as to whether or not you want to “Copy the Finder items.” For this example, I opted not to, but if you prefer to make a copy, click on Add.
6. The window you’ve dragged over will default to settings for “Add Date or Time”. We want to do this eventually, but let’s start with changing the name and adding a sequence number. In the drop-down menu at the top of the window, change “Add Date or Time” to “Make Sequential”
7. Select the radio button next to “new name”, but don’t enter a default item name.
8. Set the rest of the parameters. For my purposes, I placed the number after the name, used a dash to separate, and used a three digit number set.
9. Click on “Options” at the bottom, and select “Show this action when the workflow runs.” The application will then prompt you to fill in the item name at runtime.
A note about the date: In cases where you’d like to append a system date (e.g. Created, Modified, Last Opened or Current), you would use “Add Date or Time”. To match our file naming conventions we have already established, we’ll want to select non-space and non-special characters as our separators, use Year Month Day as the format, and click the checkbox to “Use Leading Zeros”. I would use a dash to separate the name from the date and no separator for Year Month Day. Look at the example provided at the bottom to make sure the date looks correct.
However, in my case, I’m working with a set of files where the system dates aren’t much use to me. I want to know the date of the photo; this is especially likely if I were working with scanned files from a historical period. So, I’m going to use “Add Text” instead, and append my own date.
10. Repeat step 4: drag “Rename Finder Items” to the right pane. This time, select “Add Text” from the dropdown.
11. Leave the “Add Text” field blank, click on “Options” and select “Show this action when the workflow runs.” Then, when you run the application you’ll be prompted to add text and you can append 1950, for example, to the file name.
12. Click on File > Save As, and save your Application in a location where it is easy to drag and drop files, like the Desktop. For my example, I called the application BatchFileRename.
13. Navigate to the folder containing the files you want to rename, and select them all (can use Cmd+A). Drag the whole selection to the Automator file you just created, fill in the prompts and click “Continue”.
You now have a set of renamed files. Note that the script did not modify the “Date Modified” value for the file. The script is now set up for future projects as well; any time you want to rename files, just repeat step 13.
One thing you might notice is that the date is appended after the index number. If you wanted it before the index number, we would append it to the “item name” field in the Make Sequential box and skip the Add Text section all together.
A note from a paranoid librarian: I copied this set of files from its original location to do this example, so that if something went horribly wrong, I’d still have the originals. Until you get comfortable with batch renaming you might consider doing the same.
There are lots of other uses for the Automator tool, check out “10 Awesome Uses for Automator Explained” by Matt Reich for more ideas, or do a search for Automator tutorials in your favorite search engine.
Windows – Notepad++ Column Editor
I started out hoping to accomplish this task the same way I did in the Mac OS X – with no outside tools. However, the default renaming function in Windows lacks a few things for our purposes. If you select a group of files, right-click and select “Rename”, you can rename all of the files at once.
However, the resulting file names do not conform to our earlier standards. They contain spaces and special characters and the index number is not a consistent length, which can cause sorting headaches.
After some searching, I came across this stackoverflow page, which contained a very useful command:
dir /b *.jpg >file.bat
This command allows me to dump a directory’s files into a text file which I can edit into a series of rename commands to be run as a batch file. The editing of the text file is the most time-consuming part, but using the Column Editor in Notepad++ speeds up the process considerably. (This is where we break the “no third-party tool” convention. Notepad++ is a free text editor I use frequently for writing code and highly recommend, though this process may work with other text editors.)
1. Open a command prompt.
2. Navigate to the directory which contains the files that need to be renamed.
3. The command we found above is composed of several parts. “dir” lists the directory contents, “/b” indicates to only list the filenames, “*.jpg” means to grab only the jpg files, and “>file.bat” directs the output to a file called file.bat. We are going to keep everything the same except change the name of our output file.
dir /b *.jpg >rename.bat
4. In Windows Explorer, navigate to the directory and find the file you just created. Right click on it and select Edit with Notepad++ (or Open With > Your Text Editor).
5. Put the cursor before the first letter in the first line, and open the Column Editor (Edit > Column Editor or Alt+C).
6. This tool allows you to assign the same character to every line of text in the same space. We want to insert the beginning of the Windows rename command for each line. So, in the “text to insert” box, we type:
and click OK.
7. Open the editor again to add the portion of the rename command which goes after the old filename. Here is where we’ll designate our new name, again using the “text to insert” box. I typed:
(Note, if you are using file names of varying length, move to the column after the longest file name, then use Find & Replace at the end of the process to remove the extra spaces.)
8. Next, let’s append an index before the file extension. Open the Column Editor again and this time, select the number option. Start at 1, increment by 1, and check the leading zeros box. Click Ok.
9. Last, append the file extension and end the command for each line. Using the Column Editor’s “text to insert” box one more time, add:
10. The Column Editor adds one extra line at the bottom. Scroll down and delete it before saving the file.
11. Save the file and go back to the command prompt. (If you closed it, re-open it and navigate back to the directory before proceeding.)
12. Type in the full name of the batch file so it will execute, i.e.
You’ll see the rename commands go by, and the files will each have a new name. Again, this doesn’t appear to affect the Date Modified on the file.
Windows – Batch File with Loop
It is possible to write your own batch file that will loop through the files in question and rename them. I have never written my own batch file, so in the interest of researching this post, I decided to give it a shot. There is lots of documentation available online to help in this effort. I consulted Microsoft’s documentation, DOS help documentation, and batch file examples (such as this stackoverflow post and a page on OhioLINK’s Digital Resource Management Committee wiki, which focuses preparing files for DSpace batch upload).
A batch file just groups a number of Windows commands together in one file and executes them when the batch file is run, as we saw in our previous example. But, instead of writing the specific rename commands one by one using a text editor, a batch file can also be used to generate the commands on the fly. Save the following code to a file, place it in the same directory with the set of files and then double click to run it. Caveat: test this with sample files before you use it! I have tested on a few directories, but not extensively.
First, we use @echo off to stop the batch commands from printing to the command line window.
Then, we set EnableDelayedExpansion so that our index counter will work (has to do with evaluating the variable at execution). This is why when you see i in the loops, it is written !i! instead of %i% used for other variables.
Next, I set three prompts to ask the user for some information about the renaming. What’s the root name we want to use? What’s the file extension? How many files are there? (Note, this will only work for under 1000 files). The “/p” flag assigns the response to the prompt to a variable (r, e and n, respectively). When we reference these variables later, we’ll use the syntax %r% %e% and %n%.
set /p r=Enter name root: set /p e=Enter file extension (ie .jpg .tif): set /p n=More than one hundred files? (y/n):
Next, we set the index counter, which allows to add an incrementing index to our filenames.
set /a "i = 1"
If there are less than 100 files, we only need one leading zero in the index for our first ten files, and none for the remaining. If there are more than 100, obviously we’ll want a three digit index. So, the following if statement allows us to fork to one of two loops – for two digits or three digits.
if %n%==y (GOTO three) else GOTO two
Our first segment handles three digit indexes for more than 100 files. %%v is the temporary variable that holds each item as we iterate through the loop one time. *%e% represents a wildcard plus the extension given by the user. So, if the user enters .jpg, we want to select *.jpg, or all files with a .jpg extension. Everything that follows “do” is a command.
:three for %%v in (*%e%) do (
First, we want to see if, based on the index counter i, we need leading zeros. If i is less than ten, we want two leading zeros. If it’s less than 100, we want one leading zero. This affects the renaming statement that gets applied. All of the rename statements will rename the file currently in %%v to the root name (represented by %r%), followed by a hyphen, the correct number of leading zeros, the index number (represented by !i!) and the file extension (represented by %e%).
if !i! lss 10 ( rename %%v %r%-00!i!%e% ) else ( if !i! lss 100 ( rename %%v %r%-0!i!%e% ) else ( rename %%v %r%-!i!%e% ) )
Before we exit the loop, we want to increment the index to use with the next file. And, lastly, we need to add a “goto done” statement, so that we don’t execute the “two” segment.
set /a "i = i + 1" ) goto done
The “two” section is the basically the same, except that we only need two digit indexes since there are less than 100 files.
:two for %%v in (*%e%) do ( if !i! lss 10 ( rename %%v %r%-0!i!%e% ) else ( rename %%v %r%-!i!%e% ) set /a "i = i + 1" )
We end with our “done” label, which marks the exit point.
Here is the code as a whole:
@echo off @setlocal enabledelayedexpansion set /p r=Enter name root: set /p e=Enter file extension (ie .jpg .tif): set /p n=More than one hundred files? (y/n): set /a "i = 1" if %n%==y (GOTO three) else GOTO two :three for %%v in (*%e%) do ( if !i! lss 10 ( rename %%v %r%-00!i!%e% ) else ( if !i! lss 100 ( rename %%v %r%-0!i!%e% ) else ( rename %%v %r%-!i!%e% ) ) set /a "i = i + 1" ) goto done :two for %%v in (*%e%) do ( if !i! lss 10 ( rename %%v %r%-0!i!%e% ) else ( rename %%v %r%-!i!%e% ) set /a "i = i + 1" ) :done
I saved the file as BatchRename.bat, and then copied it to my test directory. Double click on the .bat file to open it. Enter the prompts and the batch file takes care of the rest.
The files are renamed and again, the Date Modified field was not changed by this action.
Of the three methods, I slightly prefer the Automator method, because of its simplicity and ability to be re-used: once the application is created it can be used over and over again with different sets of files. The batch file for Windows is similar in that it can be re-used once created, but does require some knowledge of coding concepts. With the Notepad++ method, we have simplicity, but you’ll need to step through the file editing with each new set. I love the Column Editor, however; the Insert Number function is incredibly useful for indexing files in file names without the pesky Window parentheses.
All of the methods are quick and easy ways to rename a large set of files. And from personal experience, I will attest that all are preferable to doing it manually.
I’m curious to hear our readers’ thoughts – feel free to leave questions and other recommendations in the Comments section below.