That's a complex rename with branching/conditions so it's best done using a rename-script instead of a single regex.
I've made a script which should do what you want. It's based on the Script to perform multiple Regular Expressions but uses branching logic to make sure it only applies the most appropriate transformation to each filename.
The script is attached to the bottom of this message as a .orp file.
How to import an Opus rename preset .orp file
To load a preset into your Rename dialog, make sure the dialog is in Advanced mode, then select Import... from the File menu and select the .orp file attached to the bottom of this post. That loads the preset from disk but only temporarily. To add it to your personal list of presets, click the Add button above the list on the right and give it a name.
The script
If you want to use the script as-is, download and import the attached .orp file.
I'm just reproducing the script here so that people interested in rename-scripting can read it and use it for ideas without the hassle of downloading and importing it.
[code]@script vbscript
option explicit
Function Rename_GetNewName ( strFileName, strFullPath, fIsFolder, strOldName, ByRef strNewName )
Dim re
Dim strWordArray
Dim strExtension
Dim strNameOnly
Dim fGotMatch
fGotMatch = False
' Create a RegExp object. See http://msdn2.microsoft.com/en-us/library/ms974570.aspx
Set re = new RegExp
re.IgnoreCase = True ' Case-insensitive matching.
re.Global = False ' Don't replace all matches.
' 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
' Apply our regular expressions to the filename.
' "J Trotter - MEBulletin 4 - Qi Chong" -->
' "J Trotter\MEBulletin\04 Qi Chong"
re.Pattern = "^([^-]+) - ([^-]+) ([0-9]) - (.+)$"
if (re.Test(strNameOnly)) then
fGotMatch = True
strNameOnly = re.Replace(strNameOnly, "$1\$2\0$3 $4")
else
' "J Trotter - MEBulletin 04 - Qi Chong" -->
' "J Trotter\MEBulletin\04 Qi Chong"
re.Pattern = "^([^-]+) - ([^-]+) ([0-9]+) - (.+)$"
if (re.Test(strNameOnly)) then
fGotMatch = True
strNameOnly = re.Replace(strNameOnly, "$1\$2\$3 $4")
else
' "J Trotter - Spring Into" -->
' "J Trotter\Spring Into"
re.Pattern = "^([^-]+) - (.+)$"
if (re.Test(strNameOnly)) then
fGotMatch = True
strNameOnly = re.Replace(strNameOnly, "$1\$2")
end if
end if
end if
if fGotMatch then
' If the path matched our expected format and was changed,
' find any remaining " - " in the path and turn them into
' subfolders.
re.Global = True ' Replace all matches, not just the first " - "
re.Pattern = "^(.+) - (.+)$"
strNameOnly = re.Replace(strNameOnly, "$1\$2")
re.Global = False
' If the path matched our expected format and was changed,
' and it starts with a letter or number, move it into
' a different folder.
re.Pattern = "^(.)(.+)$"
strNameOnly = re.Replace(strNameOnly, "..\sorted\$1\$1$2")
end if
' Rejoin the name and extension and we're finished
strNewName = strNameOnly & strExtension
End Function[/code]
Multi-RegExp-nuatha.orp (2.79 KB)