Rename - Add Trailing Number

Trying to add a unique trailing number with a rename script:

[code]Option Explicit

Dim StartingNumber
Dim PadTo

' First number to try. Usually 1.
StartingNumber = 1

' Number of digits to pad the number to. For example, if PadTo
' is 3 then the script will generate 001, 002, 003, 004, etc..
PadTo = 3

Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")

Function OnGetNewName(ByRef GetNewNameData)

Dim bAbsolutePath
Dim strNum
Dim strNewName
Dim strTryNewName
Dim strTryNewFullPath
Dim bNeedLoop
Dim num
Dim i

If PadTo < 1 Then
	PadTo = 1
End If

num = StartingNumber
StartingNumber = StartingNumber + 1 ' increase for next time
strNewName = GetNewNameData.newname_stem

' Work out if the new path is relative to the original path or if it's an absolute path.
' For example, a rename from * to C:\* would move files to the root of C:\
If strNewName = fs.GetAbsolutePathName(strNewName) Then
	bAbsolutePath = True
	bAbsolutePath = False
End If

bNeedLoop = True

While bNeedLoop
	' Remove any existing number and insert the new number at the end
	strNum = CStr(num)
	If Len(strNum) < PadTo Then
		strNum = String(PadTo - Len(strNum), "0") & strNum
	End If

	for i = 1 to Len(strNum)
		if Mid(strNewName, i, 1) < "0" or Mid(strNewName, i, 1) > "9" Then
			Exit for
		end if
	if i <= Len(strNum) + 1 And Mid(strNewName, i, 1) = " " Then
		OnGetNewName = Right(strNewName, Len(strNewName) - i)
	end if

	strTryNewName = strNewName & " " & strNum

	' If new name is the same as the old name, we don't need to do anything
	if strTryNewName = Then
		bNeedLoop = False
		' Work out the full path to the new filename.
		If bAbsolutePath Then	
			strTryNewFullPath = strTryNewName
			strTryNewFullPath = fs.BuildPath(GetNewNameData.item.path, strTryNewName)
		End If

		' Check if the new filename already exists.
		If fs.FileExists(strTryNewFullPath) Or fs.FolderExists(strTryNewFullPath) Then
			'DOpus.OutputString "Already exists: " & strTryNewFullPath
			num = num + 1
			strNewName = strTryNewName
			'DOpus.OutputString "Generated name: " & strNewName
			bNeedLoop = False
		End If
	end if

OnGetNewName = strNewName

End Function

Unfortunately, it names the files as follows:

I want the name followed by 001, not the name with the 1 followed by 001. What am I missing?

No need for a script at all here. See Numbering Files.

The built-in numbering can be combined with a script if you want to do other things on top of the numbering. You'd still usually want to use the built-in numbering rather than do the numbering in the script, even if there is still a script for other things.

If you do want to do the numbering using a script for some reason, the Unique Number preset which comes with Opus has an example of how to do it. (It does numbering with a script so that it can have separate counts in each folder, instead of a single count across folders, when renaming files from lots of different places.)

Leo, thanks for the response. I used 98% of the Unique Number preset in an attempt to make this work. Just curious why the code lists both numbers. Can't figure it out...

I don't think your script is doing that. It only adds one number here.

You probably have numbering turned on in the dialog as well, so you're getting two sets of numbers.

That's it! Thanks for the clarification, Leo!