Renaming files - incrementing non-sequential numbers by one

I would like to have a button which acts as a counter after I have listened to an mp3 file.

For example:

Before: Hey Jude_n002.mp3
Press button and: Hey Jude_n003.mp3.

Can I do this with Directory Opus?

Ideally I would like to have this work on one or more selected files at a time so that:

Hey Jude_n002.mp3

Press button and:
Hey Jude_n003.mp3

I've included 'n' with the number to help distinguish it from any other digits in the filename. If you have a solution to my query, please say if this is a useful thing to do, otherwise I would be happy to just use 3 digits.

One further option which would be great. If the same or a different button could be adapted to do the same thing but for a counter at the beginning or in the middle of a filename, e.g.

n002_Beatles_Hey Jude.mp3
n005_George Michael_Faith.mp3


Beatles_n002_Hey Jude.mp3
George Michael_n005_Faith.mp3

I tried using:

Rename PATTERN='n001' TO='n002' FINDREP
Rename PATTERN='n002' TO='n003' FINDREP
Rename PATTERN='n003' TO='n004' FINDREP

This seemed to work at first but I then found that 001 was turning into 003 instead of 002, perhaps because 001 was being converted to 002 and then 003 and so on. Another issue may have been the presence of other digits in the filename, e.g. dates, track numbers, etc.

Since getting Directory Opus my understanding of regular expressions has improved but I am still hopeless at VBScript.

Any help would be much appreciated.

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

Rename PATTERN * TO *
@script vbscript
Option Explicit
' Create a RegExp object. See
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
		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.

Brilliant. Thanks very much indeed.