Rename multiple files
Renaming Procedures
In all the following examples, unless otherwise noted, it is assumed that all files in the current directory are to be renamed – ie. the first parameter is “*.*”.
If, in fact, the files to be renamed are not in the current directory, the path must be included as appropriate.
If only some files are to be renamed, the first parameter should, of course, be specified accordingly.
All the following have been tested and seem to work reliably with both Dos7 and Dos7.1 (Win95/Win98SE) but I would suggest keeping a backup of the originals before doing a complex rename of a large number of files!
To change the nth character of all filenames in the specified set, use “?”s for all preceding characters and either “?”s or “*” for following characters.
Note: it is not possible to specify characters counting back from the end of the name.
Examples:
- To change the first letter of all files in the current folder beginning with “a” to “A”:
REN a*.* A*.* - To rename “abc001.txt … abc099.txt” to “abc601.txt….abc699.txt”
REN *.* ???6*.*
(or REN abc0??.txt ???6*.*)
This is a straightforward use of REN with wildcards.
Example:
To change the names of a group of files called “abc” with different extensions to “d” with the same extensions:
REN abc.* d.*
Again, this is a straightforward use of REN with wildcards.
Example:
To change all “.txt” extensions to “doc”:
REN *.txt *.doc
Because REN has no “insert” mode, a prefix to a filename cannot be added directly. Theoretically, it should be possible to use the syntax:
LFNFOR On
FOR %v IN (*.*) DO REN %v prefix%v
but this construction is subject to the REN-bug. There are various ways to circumvent this:
- When the old filenames are all the same length, the set to be renamed can be specified with “?”s rather than a “*”.Example:
To add the characters “abc” to the beginning of each five character filename in the current directory:
LFNFOR On
FOR %v IN (?????.*) DO REN %v abc%v - If the old filenames all begin with the same character (and this is different from the first character of the prefix), that character can be included in the set to be renamed.Example:
To add the characters “abc” to the beginning of each filename beginning with “pic…”:
LFNFOR On
FOR %v IN (pic*.*) DO REN %v abc%v - If the extensions are all the same, a two stage process may be used.
- In the first pass, the prefix is added and the extensions are changed;
- In the second pass, the extensions are returned to the original.
Example:
To add the characters “abc” to the beginning of each filename (all having a “.txt” extension) in the current directory:
LFNFOR On
FOR %v IN (*.txt) DO REN %v abc%v.x
REN *.x * - Copy/Rename the files to a new temporary directory, delete the originals, and then move them back.Example:
To add the characters “abc” to the beginning of each filename in the current directory:
MD TP
LFNFOR On
FOR %v IN (*.*) DO COPY %v TP\abc%v
DEL *.*
MOVE TP\*.*
RD TP - Files can be hidden as they are renamed and then unhidden when the renaming is complete. Unfortunately I cannot see a way of doing this from the command line, but the following batch file (closely based on one published by Tom Lavedas – Ref.1) seems reliable. See RenPrefix Explained for a detailed explanation.
::RenPrefix.bat adds any number of characters ::to the beginning of a filename ::Syntax - ::RENPREFIX files-to-be-renamed prefix-to-be-added %4 LFNFOR On FOR %%v IN (%1) DO CALL %0 %1 %2 %%v GOTO:Part2 ATTRIB -h %2%1 LFNFOR Off GOTO End :Part2 REN %3 %2%3 ATTRIB +h %2%3 :End
The above lines can be cut-and-pasted into Notepad or Edit and saved as “RenPrefix.bat” (or any other name you like). It is best to avoid saving it in the directory containing the files to be renamed – it stops working if it is renamed itself while it is being used!
Example:
To add “abc” to all filenames in the current folder.
RenPrefix *.* abc - Ignore the bug and repair any misnamed files afterwards. If any double renaming is going to be very obvious, and if the renaming process is not part of some other process/batch file, then checking through the names after a rename process might be the simplest way to go. Not very elegant, and the list must be checked, but there are usually only a few files (maybe 1 to 4) misnamed.
The REN-bug makes the procedure for adding a suffix to a filename rather more complicated than it should be. Theoretically, it should be possible to use the syntax:
REN *.* ??????????suffix.*
where: suffix is the suffix to be added, and the number of “?”s equals (or exceeds) the number of characters in the longest of the old filenames. However, although most files are renamed correctly, it is almost inevitable that some of the new names will meet the specification of the files-to-be-renamed and some may end up with a double suffix. Ways to circumvent the problem are:
- When the old filenames are all the same length, the new name is specified with “?”s corresponding to the number of characters in the old filenames.Example:
To add the characters “abc” to the end of each filename (all having just five characters) in the current directory:
REN *.* ?????abc.* - If the extensions are all the same, a two stage process may be used.
- In the first pass, the suffix is added and the extensions are changed;
- In the second pass, the extensions are returned to the original.
Example:
To add the characters “abc” to the end of each filename (all having a “.txt” extension) in the current directory:
REN *.txt ????????????????abc.txt.x
REN *.x * - Copy/Rename the files to a new temporary directory, delete the originals, and then move them back.Example:
To add the characters “abc” to the end of each filename in the current directory (where the longest filename to be modified contains 16 characters or less):
MD TP
COPY *.* TP\????????????????abc.*
DEL *.*
MOVE TP\*.*
RD TP - Ignore the bug and repair any misnamed files afterwards. If any double renaming is going to be very obvious, and if the renaming process is not part of some other process/batch file, then checking through the names after a rename process might be the simplest way to go. Not very elegant, and the list must be checked, but there are usually only a few files (maybe 1 to 4) misnamed.
Removing characters from the beginning of a set of filenames can be done using a quirk in the way Dos works with spaces on the command line. The following method is a distillation of several posts to newsgroups by Tom Lavedas (Refs 2 and 3) and works by first replacing the unwanted characters with spaces, and then dropping the spaces using a FOR…IN…DO construction. Whatever the actual length of the filenames, handling of long filenames by FOR must be enabled with LFNFOR On so that the spaces can be accepted as part of the filenames.
Example:
To remove the first two characters from the beginning of all filenames in the current directory:
REN *.* " *.*"
LFNFOR On
FOR %v IN (*.*) DO REN "%v" %v
It is only possible to remove characters from the end of a filename if all of the new names are to be the same length. In this situation REN is used with the appropriate number of “?” characters.
Example:
To remove “report” from the files named “Jan-report.doc, …, Dec-report.doc”
REN *.* ???.*
To remove the “l” from files with an “.html” extension to make them “.htm”
REN *.html *.???
(or REN *.html *.htm)
This is only possible when all the characters before or after the point of insertion are the same in all the names. The exercise then becomes one of:
- Removing characters from the beginning/end of a filename (where this is possible)
- Adding a new prefix/suffix as required.
Examples:
To add a “d” after the “c” in “abc1.txt … abc65.txt”:
REN abc*.* " *.*"
LFNFOR On
FOR %v IN (" *.*") DO REN "%v" %v
FOR %v IN (*.txt) DO REN %v abcd%v.x
REN *.x *
To add a “Sales” before the “Report” in “Jan-Report.doc …. Dec-Report.doc”:
REN ??????????.doc ????SalesReport.doc
To rename files when files with the new name already exist in the same directory it is necessary to use the COPY and DEL commands.
Example:
To rename all “.doc” files in the current directory with “.bak” extensions when previous .bak versions already exist:
COPY *.doc *.bak /Y
DEL *.doc
The /Y switch for COPY suppresses the prompt for confirmation before overwriting files. If these lines are part of a batch file, this switch is not necessary.
The following procedure will rename all files in the current directory, whatever their names, in a sequentially numbered list. The order of the files in the list is best described indeterminate and depends on the order in which the files are processed by the REN *.* command. This is (perhaps?) the order the files are stored in the FAT but is unlikely to be anything simple like alphabetical, or even by date of creation.
- Prefix all files with 6 filler characters to force DOS to assign each with a short filename with the same initial characters using an appropriate method;
- Change the longfilenames to be the same as the short ones by renaming the files to the same name using a FOR…IN…DO REN construction with LFNFOR Off.
- Rename the files replacing the “~” with a “0”;
- Replace the filler characters as required.
Example:
To rename all the 45 graphics files named “dog.gif, cat.gif, etc.” in a numbered sequence of “pet001.gif” to “pet045.gif”:
LFNFOR On FOR %v IN (*.gif) DO REN %v xxxxxx%v.x REN *.x * |
Rename all files with 6 “x”s as a prefix. Note that the short filenames for the files are now “xxxxx~01.gif”, “xxxxx~02.gif”, etc. The extension had to be changed to avoid problems with the REN-bug. |
LFNFOR Off FOR %v IN (*.*) DO REN %v %v |
Without long filename support, FOR renames the files to their same (short) name. |
REN *.gif ” pet0??.gif REN ” pet0~?.gif” ” pet00?.gif” |
In two passes renames the files to replace the “x”s with two spaces followed by “pet” followed by the old dos shortname number. The second pass is needed for the numbers 1 to 9. (If there had been 100-999 files, a third pass would have been required, etc.). |
LFNFOR On FOR %v IN (” *.*”) DO REN “%v” %v |
Drop the leading spaces in the filename (see Removing characters from the beginning of a filename). |
This must be one of the most frequently asked questions in the various Dos related newsgroups!
Although I am sure that it is possible to rename files to include either the current date or the file-last-modified date, the only way to do this in pure Dos would be with a complex batch file.
A recent (Feb ’00) discussion on this subject in news:alt.msdos.batch generated a consensus that this was simply not a worthwhile exercise. There are a number of freeware/shareware programs (see Links) available that can be used either as standalone utilities or incorporated into a batch file for a custom application. I have no experience in using any of these.