Renaming files - incrementing non-sequential numbers by one

Here's the code for a button which does it:

Rename PATTERN * TO *
@script vbscript
Option Explicit
' Create a RegExp object. See http://msdn2.microsoft.com/en-us/library/ms974570.aspx
Dim re
Set re = new RegExp
re.IgnoreCase = True
re.Global = False
re.Pattern = "(.*(^|[^a-zA-Z0-9])n)([0-9]+)(($|[^a-zA-Z0-9]).*)"
Dim PadTo
PadTo = 3 ' Pad numbers to three digits, e.g. "001"
Function Rename_GetNewName ( strFileName, strFilePath, fIsFolder, strOldName, ByRef strNewName )
	Dim strExtension
	Dim strNameOnly
	Dim num
	Dim strNum
	' If we're renaming a file then remove the extension from the end and save it for later.
	If fIsFolder or 0 = InStr(strFileName,".") Then
		strExtension = ""
		strNameOnly = strFileName
	Else
		strExtension = Right(strFileName, Len(strFileName)-(InStrRev(strFileName,".")-1))
		strNameOnly = Left(strFileName, InStrRev(strFileName,".")-1)
	End If
	If re.Test(strNameOnly) Then
		num = re.Replace(strNameOnly, "$3")
		num = Int(num) + 1
		strNum = CStr(num)
		If Len(strNum) < PadTo Then
			strNum = String(PadTo - Len(strNum), "0") & strNum
		End If
		strNameOnly = re.Replace(strNameOnly, "$1" & strNum & "$4")
	End If
	' Rejoin the name and extension and we're finished
	strNewName = strNameOnly & strExtension
End Function


See also: Increase & decrease number which does something slightly different.