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
Else
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
Next
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 = GetNewNameData.item.name Then
bNeedLoop = False
else
' Work out the full path to the new filename.
If bAbsolutePath Then
strTryNewFullPath = strTryNewName
Else
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
Else
strNewName = strTryNewName
'DOpus.OutputString "Generated name: " & strNewName
bNeedLoop = False
End If
end if
Wend
OnGetNewName = strNewName
End Function
[/code]
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?