Renaming Multiple Files At Once (Windows)

The Windows Explorer allows files to be renamed easily, by single clicking on the name and then editing it; however, this can be quite tedious if there are many files to be renamed. (Attempting to select a range of files and then using right-click + Rename doesn’t work. Only the last file in the range is renamed.)

Command Prompt to the rescue. This tip will show you how simple it is to use the REN command in the command prompt to do a wildcard rename.
REN, of course, is short for rename. The REN takes the form of “REN oldfilename newfilename” (or, you can spell out RENAME, if you prefer). Both the old filename and the new file name can contain wildcards.

Change Filename Extensions: Say, for example, you obtain a large set of ASCII files, all of which have names that end with a “.ASC” filename extension, but you need them to be named with “*.TXT” extensions instead. (In the following illustrations there are only two such files, but imagine that there are hundreds.)

DOS REN command

Quirky Wildcard Meanings: When wildcards appear in the old filename, they hold the usual wildcard meanings. A question mark matches exactly 1 character, while an asterisk matches any number of characters. The wildcard matching algorithm is a bit quirky when it comes to how the wildcards are treated in the new filename, though. This is a holdover from the ancient days of DOS when filenames were restricted to 8.3 file name (a maximum of eight characters for the name, and a maximum of three characters in the one-and-only filename extension). To make a long story short, the REN command only deals with whole parts when building the new filename (a part being either the filename to the left of the rightmost period, or the extension to the right of the rightmost period).

So, the following three commands are actually equivalent:

REN H*.asc H*.txt
REN H*.asc *.txt
REN H*.asc G*.txt

They all say, “Find any files that begin with H and end with .asc, and rename them so that they end with .txt”. In all three cases, when the rename command sees the asterisk in the name part of the new name, it assumes that any accompanying characters (H, G) are irrelevant and that what you really mean is for the new name to propagate whatever was found as the name part of the old name, and to just change the extension. Weird, huh?

Removing Filename Extensions: A common situation is that a file will have a name that ends with two filename extensions, often because some automated process added the second extension. (We showed one such process for converting Microsoft Word documents to plain ASCII text that resulted in file names such as “Huckleberry_Finn.doc.txt”. It is also quite common for backup copies of files to be saved by tacking on “.bak” to any existing filename extension, as in “Huckleberry_Finn.txt.bak”.) Again, imagine that there are hundreds of such files that need to be renamed so that they end in only the one filename extension.

There is a bit of a trick to removing such extraneous filename extensions with the REN command. You’d think that the logical thing to do would be to issue a command such as this:

REN *.bak *

Unfortunately, as we mentioned above, the two wild cards don’t really correspond. It is another quirk of the rename command to that when the filename pattern for the new filename does not contain any period, then it is assumed that the pattern refers to the entire filename with extension. Thus, a new filename pattern of just “*” will match the entire filename that was found. In other words, it renames the file from what it was to what it was (i.e. does nothing).

So, the trick is to go ahead and add the period, like this:

REN *.bak *.

Now, the asterisk in the new name only corresponds to the filename part of the old name. The “.bak” is dropped in favor of the nothing that is to the right of the period in the new filename pattern. Hopefully, I have not confused you by the fact that, in this case, “the filename part” refers to everything up to the rightmost period. So, in “Huckleberry_Finn.txt.bak”, the filename part is “Huckleberry_Finn.txt” and the extension part is “bak”.

Related articles:

Trackbacks & Pings

Comments

  1. Hmm, in WinXP Pro and Vista, highlighting a group of files and chooses File->Rename, or right-click->Rename or F2 all allow me to rename all of the files to a single name and Windows will append (copynum) to each file. Do I have some secret, fancy, special tool installed?

  2. mawes, XP, and I gather Vista, rename files through the GUI but as I recall it ’s rather limited. The REN fucntion allows the use of wildcards and extension changes amongst other possibilites I believe.

  3. Your timing is impeccable, thanks for the tip!

  4. Mawes: That’s interesting. I don’t see much use for that copynum behavior — well, maybe for obfuscating a set of filenames. I must not have actually tried having the GUI rename multiple files to a fixed name, and just assumed it would only rename the last file. (I probably got as far as not being able to enter a wildcard in the new name, gave up, and hit Cancel.) Thanks for the clarification.

  5. I can also already select a group of files and rightclick rename them, with a number appended to the end. I have Windows XP, maybe it doesn’t work in windows home.

  6. This was very help ful for me

    Thanks

  7. what if you have characters in the beginning of a filename that you would like to del? I’m using this app to convert some files and it spits out [extra]-filename.exe and what I’m hoping to do is take out the “[extra]-” out of the file… it seems that whenever I use the ren command to do this it does not del the first char, but simply renames them….
    ex c:\ren [extra]-*.exe *.exe
    will obviously not change anything but if you change the char it will overlap
    ex c:\ren [extra]-*.exe 123*.exe
    will change the files to 123tra]-filename.exe
    thanks for your time…

Post a Comment


Your email is never published nor shared. Required fields are marked *



© 2006-2007 Maxim Software Corp.  All rights reserved.