Tuesday, April 28, 2009

How to Rename Files En Masse

Recently, I was confronted with a rather large batch of image files that needed renaming. There were several hundred, and it would have taken far too long to do it manually. So, I wrote a short Python computer program that will do this in a matter of seconds.

If you also ever need to rename many files and don't want to do it manually, you can use this same technique (no experience necessary).

This example is provided as is. Please don't send me your angry email if you accidentally rename all the files on your computer and nothing works anymore. Follow the directions carefully.

Follow these steps.

1) Make a complete copy of all the files that need renamed and save them to a new folder. You can't undo this renaming thing so you don't want to take the chance that all your files get renamed incorrectly. Back them up and be safe. Nothing but the files to be renamed should be in the new folder. Call the folder "rename"

2) Download Python from their website and install it: http://www.python.org/

3) Download Komodo Edit from their website and install it: http://www.activestate.com/komodo_edit/

4) Read Chapters 1-3 of Bill Turkel's "Programming Historian" and set up Komodo Edit to have a 'Run Python' feature (it's in the chapter. Read it! It will only take 10 minutes).

5) Open Komodo Edit and create a new file. Save it as Rename.py .

6) Paste the following code into that file. Indenting is important:


#!/usr/bin/env python
import os
from os import rename

x = os.listdir('C:\\Documents and Settings\\Replace with Path to Folder')
print x
a = 0
b = 0
for file in x:
    if x[a].find(".tif") != -1:
        print a
        os.rename(x[a], 'newName-' + str(b) + '.tif')

#end code

7) Three lines need to be changed.
a) x = os.listdir('C:\\Documents and Settings\\Replace with Path to Folder')
change this to have the path of your folder containing the files to be changed. Make sure you use two slashes between directory layers, not one. The first slash is actually an "escape" character that tells the program to consider the 2nd slash as an alphanumeric character rather than a code character with special meaning.
You can find the path of your folder by right clicking it and selecting properties.

b) if x[a].find(".tif") != -1:
Replace ".tif" with whatever type of file you are changing the name of. This can be any file type you like. .jpg, .txt, .html, etc.

c) os.rename(x[a], 'newName-' + str(b) + '.tif')
Replace "newName" with whatever you want to change the name to, and ".tif" to whatever file format you selected in step (b). This is a pretty simple program - if you go through it line-by-line you can probably figure out what each step does.

8) Resave the file. Place Rename.py in the same folder as the files that you want renamed. Treat this program like a loaded gun. You don't want to run it in the wrong folder. If you run it will rename everything in that folder whether you want it to or not. As long as you don't double click it you should be ok.

9) Open Rename.py in Komodo Edit and run the program using the technique described in the Programming Historian's "Hello World" example.

You should now have an entire folder full of renamed files. They will be named "newName1.tif", "newName2.tif", etc. Notice that the old names are now completely gone.

This can save you a lot of time if you're dealing with hundreds of files. But, please be careful. Don't rename any files that are critical to your system.

A little bit of computer programming can go a long way

No comments: