Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Volume File Renaming for the Eccentric

4.47/5 (13 votes)
19 Sep 2008CPOL11 min read 1   732  
Renaming files with a multi-file renamer.

Image 1

Introduction

Multi-File Renamer is a simple Windows Forms application I created to assist me with renaming thousands of files. It's as simple a program as one could imagine, and it's not cluttered with difficult user selected options and other garbage that you see in most file renaming tools. The things you can do with this are:

  • Replace strings in file names (supports RegEx)
  • Add prefix to files names
  • Add suffix to files names
  • Change file names to uppercase
  • Change file names to lowercase
  • Change file names to proper (title) case
  • Insert/Increment numbers into file names
  • Includes a simple Regular Expression editor

Background

You may know someone like me; methodical, obsessive, and compulsive. The kind of person who becomes visibly unstable at the sight of things not being orderly. This sort of "neat" compulsion extends to the nth degree when it comes to file systems and directory hierarchies. For example, one might not find fault in a folder storing MP3 music files called Music where each file is named something like:

[artist] - [album] - [Track].mp3 
    (e.g. Megadeth - Rust In Peace - Take No Prisoners.mp3).

However, for me personally, this is a crime against nature, since, to me, it's obvious that tracks should be sorted by:

[Genre]\[Artist]\[(Year) Album]\[Track]!

Likewise, a folder containing Media Center/Tivo recordings should be in a directory like:

[Entertainment]\[Video]\[TV Recordings]\[Series]\[Season]\[Episodes]

Unfortunately for me, my preference in file names and directories change from time to time, and this leaves me in a situation where I have to rename massive amounts of files. I can't count the amount of scripts I've written to rename files in various languages throughout the years. However, I had a catastrophic loss of source code when I inadvertently discharged static electricity on a hard disk that I used to archive code last year, and I lost it all. This tragic loss has prompted me to start all over and create a new renaming tool.

Become OCD with your File System

How are you going to know that you have files that need to be renamed obsessively unless you can browse them easily? You certainly aren't going to troll around in Explorer, browsing folders for hours on end, looking for illogically named files, are you? No, that sort of exercise is reserved for the most obsessed individuals, like a less IT skilled me. You need an index!

The pitiful MS Windows Search feature (and I use the term search loosely) is not going to cut it as an index. It's just too limited. I kill two birds with one stone by making my servers index all the files on their shares every night and placing that index in the root of the share. By doing this, I can browse the entire file system of a share in my psychotic hunt for poorly named files, and my users can search the contents of the share with Notepad or with a script hosted on our web server (supporting Regular Expressions).

@echo off
echo Generating Indexes for Ebooks
dir /B /S \\baileyfs01\Files\EBooks\*.*  > 
          \\baileyfs01\Files\EBooks\e-books_db.txt

echo Generating Indexes for Applications
dir /B /S \\baileyfs01\Files\Applications\*.* > 
    \\baileyfs01\Files\Applications\applications_db.txt

echo Generating Indexes for Music
dir /B /S \\baileyfs01\Music\*.* > \\baileyfs01\Music\music_db.txt

echo Generating Indexes for Photos
dir /B /S \\baileyfs01\Files\"Digital Photos"\*.* > 
    \\baileyfs01\Files\"Digital Photos"\photos_db.txt

echo Generating Indexes for TV
dir /B /S \\baileyfs01\Files\Entertainment\Video\"TV Recordings"\*.* > 
    \\baileyfs01\Files\Entertainment\Video\"TV Recordings"\tv_db.txt

echo Generating Indexes Of User Downloads
dir /B /S \\baileyweb01\Downloads\*.* > \\baileyweb01\Downloads\udl_db.txt

With your indexes in hand, you can now browse the entire set of files in an orderly manner and make quick decisions in terms of naming standards. Once you've identified the files that violate your convention, you can fire up the multi-file renamer tool and rename them appropriately.

Using the Program/Code

The code is nothing spectacular, and it certainly violates traditional design practices, as most code does when you write it for your personal use in a hurry. Using the program is really simple. Basically, it works like this:

  1. Navigate to the directory with the files you need to rename
  2. Place a check-mark next to the files you want to rename
  3. Switch to the Rules tab and define the rules to use in the operation
  4. Click the 'Preview' button to see how the changes will look once applied
  5. Apply the changes

Inserting and Incrementing Numbers

One of the most useful features of this tool is the number incrementing feature. When used with Regular Expressions, the numbering function gives you a high degree of flexibility when renaming files in batch. While setting your rename rule, you can insert the wildcard string %n[Number] to define an incrementing operation.

Suppose, for example, that you have an HTPC (Home Theatre PC) setup or Media Center application like XBOX Media Center that can leverage scraping and web services technologies to aggregate information about the media such as series, episode, and actor summaries. Most of these systems require a strict naming standard in order to properly distinguish files. Usually, it's something like Season#, Episode Number, Series - Title (e.g., S02E01 Tales From The Crypt - Dead Right).

If the files you have now are named something like this: 01 Dead Right.mpg, then you need to rename the file 01 Dead Right.mpg to S02E01 Tales From The Crypt - Dead Right.mpg, and all the rest of the files in the folder need to be renamed with the episode being incremented (S02E02, S02E03, S02E03, etc., etc.). To do this, you select the 'Replace' option and replace:

[\d]+ -

with:

S02E%n[1] Tales From The Crypt -

This will rename the file above to S02E01 Tales From The Crypt - Dead Right.mpg. The next file would be named S02E02 Tales From The Crypt - The Switch.mpg, etc., etc., etc.

You can increment anywhere in the filename that you'd like as long as you can match a pattern. If there is no pattern to match, then you can use the 'Move Up' and 'Move Down' buttons to re-order the files in a user specified order, and add a prefix using the Prefix rule. When you have a prefix, you now have a pattern you can match to increment numbers. This is especially useful for music files where tracks in a CD are not named with the track number in the file name.

Practical Application

I'm going to demonstrate a few scenarios I've used the utility for by media type, so you can get a better understanding of why this sort of thing is helpful.

Video/TV Recordings

As we all know, disc space is limited for home users, and the ability to store every Tivo Recording, or TV-DVD Season rip on a server hard disk is just not realistic. At some point, the decision must be made to either delete the content from the server or select out the very best episodes. I violate my own rules when this time comes, because I don't like to leave one or two episodes in its season folder if the complete season is not contained within. I'd rather rename the files and move them to a root folder. So, for example, suppose we have Tales From The Crypt Season 2 on the server, and we've watched all the episodes. Time has come to pick out the ones we want to keep and delete the ones we don't want to keep.

G:\Files\Entertainment\Video\TV Recordings\Tales From The Crypt\Season 02\S02E01 - 
                            Dead Right.avi
G:\Files\Entertainment\Video\TV Recordings\Tales From The Crypt\Season 02\S02E02 - 
                            The Switch.avi
G:\Files\Entertainment\Video\TV Recordings\Tales From The Crypt\Season 02\S02E03 - 
                            Cutting Cards.avi
G:\Files\Entertainment\Video\TV Recordings\Tales From The Crypt\Season 02\S02E04 - 
                            Til Death.avi
....

In this case, we want to remove the season identifier tag and change the names to something more friendly such as the name of the show and its title. Of course, if you're using a Media Center application, you wont want to remove this tag because the data related to it is a dependency. For the sake of demonstration, using the Multi-File Renaming Utility, we:

  1. Browse to the folder with the files to rename
  2. Place a checkmark next to the episodes we want to keep and rename (e.g., Season 2, episodes 1-4)
  3. Click the 'Rules' tab and check the Replace checkbox
  4. Enter the value [SE][0-9].+ - into the first combo box
  5. Enter the value Tales From The Crypt - into the second combo box
  6. Click the 'Preview' button and validate the previewed file names
  7. Click the 'Rename' button to apply the change
  8. Right-click any checked item, and select the 'Move Files' option from the context menu to move the renamed files
  9. Use Explorer to delete the directory once the renamed files have been moved to a new location

Music Files

Music files have been the bane of my existence for many, many years. Over the years, I have amassed an enormous music library from 20 years of buying CDs. With the emergence of ID3Tags and great multimedia applications to leverage them (XBMC, Amarok, MP3-CMS), file names are less important than they used to be in the good 'ole days of WinAmp when you browsed to a folder and loaded everything into the player. If the music files did not follow a strict convention, then the tracks would not line up correctly, and you'd have to toggle various sorting options to try to get songs in the same album to display in the correct order.

This need for files to line up properly forced a lot of us to use naming schemes like Year_TrackNumber_Artist_Album_SongName. When thousands of files started cluttering up single folders, we changed this scheme to create a directory scheme like Year\Artist\Album\TrackNumber_SongName.mp3. Many variations of this scheme exist, and I have tried just about every one of them. Each time I decide that my current structure doesn't work best, I'm forced to rename 80GB+ worth of music files :(. Over the years, I have finally settled on a personal standard, with a directory structure like Genre\Artist\(Year) Album\Track Number Artist - SongName.mp3.

G:\Music\Rock\Megadeth\(1986) Peace Sells - Remaster\01 Megadeth - Wake Up Dead.mp3
G:\Music\Rock\Megadeth\(1986) Peace Sells - Remaster\02 Megadeth - The Conjuring.mp3
G:\Music\Rock\Megadeth\(1986) Peace Sells - Remaster\03 Megadeth - Peace Sells.mp3
G:\Music\Rock\Megadeth\(1986) Peace Sells - Remaster\04 Megadeth - Devils Island.mp3
...

At one time, these files looked like:

G:\Music\Rock\Megadeth\02 Peace Sells ...But Who's Buying\01_megadeth_wake_up_dead.mp3
...

To convert these names to our new convention, we follow these steps:

  1. Replace the string _megadeth_ with Megadeth -
  2. Replace all underbars (_) with an empty string
  3. Convert the file name to proper (title) case

Digital Photos

As with music files, digital photos have been problematic for me from the day I got my first digital camera back in 1999. How should they be organized and categorized? What directory structure would normalize the files so they're easy to find but not excessively scattered about the hierarchy. For the moment, I've settled on a base structure like this:

G:\Files\Digital Photos\Events
G:\Files\Digital Photos\Family
G:\Files\Digital Photos\Friends
G:\Files\Digital Photos\Holidays
G:\Files\Digital Photos\House
G:\Files\Digital Photos\Projects
G:\Files\Digital Photos\Vacations
G:\Files\Digital Photos\Vehicles
G:\Files\Digital Photos\Wallpaper

All these directories have their own sub-hierarchies as well, which are organized in the most logical way I see possible.

Modern Operating Systems include a digital camera and scanner wizard utility that will name your photo files when you import them, and you should think carefully about this name before doing the import. If you are eager to see the photos and don't want to bother with a logical naming system, then you'll end up with file names like this:

G:\Files\Digital Photos\Vacations\DEFCON 16-2008 - Vegas\DSCF5501.JPG
G:\Files\Digital Photos\Vacations\DEFCON 16-2008 - Vegas\DSCF5502.JPG
G:\Files\Digital Photos\Vacations\DEFCON 16-2008 - Vegas\DSCF5503.JPG
...

These particular photos are of the Network Operations Center at DEFCON 2008. To convert these files to a logical naming scheme, we replace the string DSCF55 with NOC- to get these file names:

G:\Files\Digital Photos\Vacations\DEFCON 16-2008 - Vegas\NOC-01.jpg
G:\Files\Digital Photos\Vacations\DEFCON 16-2008 - Vegas\NOC-02.jpg
G:\Files\Digital Photos\Vacations\DEFCON 16-2008 - Vegas\NOC-03.jpg
...

Digital E-Books

E-books is another category that changes a lot for me. For the root folder structure, I've settled on:

G:\Files\EBooks\Comics
G:\Files\EBooks\Computer
G:\Files\EBooks\Science
G:\Files\EBooks\Fiction & Non
G:\Files\EBooks\Tips & Tutorials

At one time, I wanted all my e-books to be prefixed by a year value, so if I was looking for a specific topic such as C# programming, I would see the most recent text first.

G:\Files\EBooks\Computer\Programming\Visual Studio .NET\CSharp.NET\(2002) 
            Database Programing With CSharp.pdf
G:\Files\EBooks\Computer\Programming\Visual Studio .NET\CSharp.NET\(2004) 
            CSharp Threading Handbook.chm
G:\Files\EBooks\Computer\Programming\Visual Studio .NET\CSharp.NET\(2004) 
            Dissecting a CSharp application.pdf
G:\Files\EBooks\Computer\Programming\Visual Studio .NET\CSharp.NET\(2006) 
            Expert CSharp 2005 Business Objects, 2nd Edition.pdf
...

However, now, I feel it's more fashionable to prefix the title with the publisher name. This task was difficult because I had to first organize the files into folders representing each publisher. Once that was done, I replaced the year prefix \(\d+\) with the publisher name (e.g., OReilly).

G:\Files\EBooks\Computer\Hacking\OReilly Amazon Hacks (2003).chm
G:\Files\EBooks\Computer\Hacking\OReilly BlackBerry Hacks (2005).chm
G:\Files\EBooks\Computer\Hacking\OReilly BSD Hacks - 100 Industrial-Strength 
                        Tips & Tools (2004).pdf
G:\Files\EBooks\Computer\Hacking\OReilly Car PC Hacks (2005).chm
...

Comic Books

One common problem with comic archives is similar to what you see below:

G:\Files\EBooks\Comics\Retail\Image Comics\The Walking Dead\The Walking Dead #1.cbr
G:\Files\EBooks\Comics\Retail\Image Comics\The Walking Dead\The Walking Dead #10.cbr
G:\Files\EBooks\Comics\Retail\Image Comics\The Walking Dead\The Walking Dead #11.cbr
...

Obviously, issue #10 should not follow issue #1. To resolve this, we

  1. Check the issues that are missing the preceding zero (issues 1-9)
  2. Create a rule to replace the string Dead # with Dead #0
  3. Preview and apply the change

The Walking Dead #1.cbr becomes The Walking Dead #01.cbr.

In addition, when I create cbr/cbz files from scans, I wait to rename them from zip/rar to cbz/cbr until I'm done scanning for the day, and I use the renamer to batch modify the names.

Emulator ROMs

I've seen all kinds of naming conventions for emulator ROMs, but one I see often is all uppercase:

Y:\Entertainment\Games\Emulators\Nintendo Entertainment System\Roms\OLD\10_YARD_FIGHT.NES
Y:\Entertainment\Games\Emulators\Nintendo Entertainment System\Roms\OLD\1942.NES
Y:\Entertainment\Games\Emulators\Nintendo Entertainment System\Roms\OLD\8_EYES.NES
Y:\Entertainment\Games\Emulators\Nintendo Entertainment System\Roms\OLD\ADDAMS_FAMILY.NES
Y:\Entertainment\Games\Emulators\Nintendo Entertainment 
            System\Roms\OLD\ADVENTURES_IN_THE_MAGIC_KINGDON.NES
Y:\Entertainment\Games\Emulators\Nintendo Entertainment 
            System\Roms\OLD\ADVENTURES_OF_BAYOU_BILLY.NES
...

For this sort of naming scheme, I perform these steps:

  1. Replace _ with [empty space]
  2. Convert to lowercase (you can't go to ProperCase formatting from All Caps)
  3. Convert to ProperCase

Which results in:

Y:\Entertainment\Games\Emulators\Nintendo Entertainment System\Roms\OLD\10 Yard Fight.nes
Y:\Entertainment\Games\Emulators\Nintendo Entertainment System\Roms\OLD\1942.nes
Y:\Entertainment\Games\Emulators\Nintendo Entertainment System\Roms\OLD\8 Eyes.nes
...

Something else I've seen a lot is:

Y:\Entertainment\Games\Emulators\Nintendo 64\Roms\Banjo-Kazooie (U)[!].v64
Y:\Entertainment\Games\Emulators\Nintendo 64\Roms\Blast Corps (U)[!].v64
Y:\Entertainment\Games\Emulators\Nintendo 64\Roms\Body Harvest (U)[!].v64
...

I'm not really sure why the [!] strings are in these file names. All these emulator ROMs came with a MAME arcade cabinet I had years ago, and I always wondered why the seller used file names like this, that, seemingly makes no sense. To get rid of these illogical exclamation points, we just replace [\[!\]] with [empty string].

Conclusion

I'm not ashamed and I'll come right out with it; I'm embarrassed by this code! I've written C scripts that are more elegant. However, I make no apologies since I put this together in about 3.5 hours because I found some DVD-rips on one of my servers that violated my strict rules, and I couldn't deal with the horror of allowing it to either remain there mocking me, or rename each file one by one!

History

  • 19 September, 2008: Article submitted.
  • 20 September, 2008: Fixed threading issue by using BackgroundWorker.
  • 20 September, 2008: Added XML persistence.
  • 20 September, 2008: Added Explorer treeview.
  • 21 September, 2008: Added number incrementing feature.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)