Add a Unique Number while renaming

See Also: Number Within Sub-Folders rename script

Summary:

The script will find a pattern -- [NUM] by default -- in the filename and replace it with the next number in the sequence that isn't already being used by another filename in the directory.

Use the Old Name and New Name fields in the Opus rename dialog to add [NUM] into the new name. Remember that you can still use the Enable file information fields option to insert metadata, such as {shootingtime} (when a photo was taken) into the new filename.

Further Detail:

The rename dialog has an option, Automatically rename if new filename exists, which adds a number to the end of the filename if there is already a file with the generated name. This works fine in most cases but Opus doesn't currently provide a way to specify how the number is added to the filename.

The rename dialog also has a Sequential numbering feature which does give you control over the format of the generated numbers. However, this feature generates a sequence of numbers that increases for each file that is renamed in a single operation, without paying attention to the numbers used by any existing files. Again, this is useful in many situations but not suitable for others.

This rename script effectively combines the two features into one. It lets you specify where the number is inserted into the new filenames and how it is padded (e.g. 001 instead of 1). It also chooses numbers based on the existing filenames in the same directory so that the new name uses the lowest possible positive number that results in a unique filename.

This script also has a Random Numbers option which lets you insert a random number into the filename while still checking that it doesn't generate a name that is already in use.

Note: The preview in the Rename dialog may display several files with the same number. This is normal and does not reflect what will happen if you go ahead with the rename. (The reason is obvious: The script works by testing whether the names already exist on disk but just doing a preview doesn't rename anything, so the filenames in the preview do not affect each other like they will when you do the rename for real.)

Configuration:

Near the start of the script there are three things which you can configure:

  • NumberPattern = "[NUM]"
    Change this if you want to use a different string to indicate where the number needs to go in the new filename. (e.g. If you need "[NUM]" to appear in the name literally.)

  • StartingNumber = 1
    Change this if you want the count to start at a number other than 1. (e.g. If you want to start counting at 0.)

  • PadTo = 3
    Change this if you want the number to be padded to a different number of digits. The default of 3 means that 1, 2, 3, 4 will be turned into 001, 002, 003, 004, etc.. Change it to 1 to avoid padding completely.

  • RandomNumbers = False
    Set this to true if you want the script to generate random numbers instead of a sequence of numbers. If RandomNumbers is True then the PadTo setting defines the range of random numbers. If PadTo is 1 then you will get numbers from 0 to 9. If PadTo is 3 then you will get numbers from 000 to 999.

Also, remember to change the Old Name and New Name fields to match what you're trying to do. Don't forget that you can still use wildcards/find-and-replace/regular-expressions as well as all of the file information fields and other options provided by the Rename dialog. The results of those things are fed into the script which then makes additional changes to the filename.

Download:

Examples:



I've added a new option to this rename script:

  • RandomNumbers = False

Edit the script and set this to true if you want the script to generate random numbers instead of a sequence of numbers. If RandomNumbers is True then the PadTo setting defines the range of random numbers. If PadTo is 1 then you will get numbers from 0 to 9. If PadTo is 3 then you will get numbers from 000 to 999.

1 Like

Anyone care to add functionality where

  • IF the filename with the StartingNumber does not exist already (ex. filename001 DNE)
  • THEN entirely skip renaming and remove the NumberPattern "[NUM]" from the new filename?
2 Likes

I also am looking for a way to do this.

Only start adding the unique numbers if the first non-numbered doesn't exist.

Simply not running the script is not an option?