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.)
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”.