Renaming Multiple Files

Ren (Rename)
Renaming Multiple Files

Index

Renaming multiple files in Dos can be simple or complicated depending on exactly what is you want to do. In ALL cases it is most important to have a good understanding of the logic used by Dos to determine which files are to be renamed and what the end result is to be. Dos's logic is not what I would call intuitive!

Contents

  1. Substituting one or more characters for others
  2. Renaming a set of files that have the same name but different extensions
  3. Renaming extensions of a set of files
  4. Adding a prefix
  5. Adding a suffix
  1. Removing leading characters
  2. Removing trailing characters
  3. Adding/Removing middle characters
  4. Renaming with names already in use
  5. Renaming in a numbered series
  6. Renaming with date stamp


The Fundamental Rules of REN

  1. Long filenames containing spaces must be enclosed in inverted commas.

  2. REN will accept either short or long filenames in the first parameter.

  3. The first parameter selects the files to be copied. It has no effect on the new names to be assigned. Incidentally, it might be worth noting that *.* will not only select all files in the directory, it will also include subdirectories. If subdirectories are not to be renamed, they can be temporarily hidden (using ATTRIB) or, if all the files have extensions and the directories don't, using *.?*" will work.

  4. The second parameter determines the new name to be given to the selected file(s). Unless otherwise specified, characters for the new name are copied character-bcharacter from the old name. ie. REN works in overwrite mode - there is no insert mode.

    If the second parameter is:
    *.*all characters of the selected files will be used for the new name (i.e. the name remains the same).
    b*.*the first character of the name will be changed to a "b"; subsequent characters will remain the same.
    " *.*"the first character will be changed to a space
    ?b.*the second character of the name will be changed to a "b"; all other characters will remain the same.
    *b.*same as *.*; the "b" is ignored and not added to the end of the original name.
    ????.*The filename will be truncated after the first four characters.

  5. The REN-bug: There is a bug associated with REN that can complicate matters when renaming multiple files. The problem arises when the new names meet the specification of the first parameter - ie. the files to be renamed - and some of them get renamed twice (or even three times). In some cases this behaviour is irrelevant: eg. if the first character of all files is to be changed to "A" then changing it to an "A" again doesn't matter. On the other hand, it is noticeable if an "A" is to be added to the beginning of the filename and some files end up with "AA" or even "AAA". The behaviour does not seem to be particularly consistent but, once aware of the problem, it is usually possible to avoid its effects.


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!


  1. Substituting character(s)

    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:

    1. To change the first letter of all files in the current folder beginning with "a" to "A":
      REN a*.* A*.*

    2. To rename "abc001.txt ... abc099.txt" to "abc601.txt....abc699.txt"
      REN *.* ???6*.*
      (or REN abc0??.txt ???6*.*)


  2. Renaming a set of files all with the same name but different extensions

    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.*


  3. Changing the extensions of a set of files

    Again, this is a straightforward use of REN with wildcards.

    Example:
    To change all ".htm" extensions to "htm":
    REN *.htm *.htm


  4. Adding a prefix

    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:

    1. 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

    2. 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

    3. If the extensions are all the same, a two stage process may be used.
      1. In the first pass, the prefix is added and a new extension is added;
      2. In the second pass, the new extensions are removed leaving 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 *

    4. 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

    5. 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 by Tom Lavedas) seems reliable. See Renprefix.bat explained for a detailed explanation.
      ::RenPrefix.bat adds any number of characters
      ::to the beginning of a filename
      ::Syntax -
      ::RENPREFIX files-to-be-renamed prefito-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

    6. 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.


  5. Adding a suffix

    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:

    1. 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.*

    2. If the extensions are all the same, a two stage process may be used.
      1. In the first pass, the suffix is added and the extensions are changed;
      2. 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 *

    3. 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

    4. 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.


  6. Removing characters from the beginning of a filename

    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 a method by Tom Lavedas 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


  7. Removing characters from the end of a filename

    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 ".htm" extension to make them ".htm"
    REN *.htm *.???
    (or REN *.htm *.htm)


  8. Adding/Removing characters to/from the middle of a filename

    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:

    1. Removing characters from the beginning/end of a filename (where this is possible)
    2. 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


  9. Renaming files with names that are already in use

    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.


  10. Renaming files as a numbered series

    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.

    1. 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;
    2. 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.
    3. Rename the files replacing the "~" with a "0";
    4. 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 *.x " 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 leading characters).


  11. Renaming files with a date stamp

    This is one of the most frequently asked questions!

    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.

    the general concensus on this subject is that this is simply not a worthwhile exercise. There are a number of freeware/shareware programs 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.


This page last revised:
February 12, 2000.