Rename scripting lets us use languages like VBScript and JScript to create quite complicated rename presets.
This TitleCase preset/script can do various useful filename cleanup tasks at the same time:
- Replace %20 with spaces.
- Remove multiple spaces.
- Replace _ with a space.
- Strip leading/trailing spaces.
- Make extension lower case.
- Convert to true titlecase.
- Capitalise after parenthesis.
- Capitalise after a dash.
- Capitalise after a full stop.
- Capitalise after first non-numeric character.
There are two versions of the script:
-
For Opus 12 & above:
If you are using Opus 12 or above, use the enhanced version of the script. A number of checkboxes will be added to the Rename dialog to let you toggle the script's options on and off:
If you change one of the script's checkboxes in Opus 12, and want that to be the default from then on, just re-save the preset. (Quickest way: Ctrl+S in the Rename dialog, after toggling the checkbox.)
-
For Opus 11 & earlier:
If you are using the version for Opus 11 & earlier, you can turn options on and off by editing the top of the script. Change the variables from True to False to toggle the options:
Download:
- Opus 12 & above: Titlecase (Opus 12).orp (8.4 KB)
- Opus 11 & earlier: Titlecase.orp (7.6 KB)
- Import the preset as described in How to use Rename Presets from this forum.
Script code:
If you just want to use the script, download the .orp preset file above. The script code is reproduced below to help people browsing the forum looking for scripting techniques.
This is the "Opus 12 enhanced" version of the script.
Option Explicit
' ********************************************************
' TitleCase by Steve Banham (tanis@dopus.com) 07-Apr-2007
' Enhanced for Opus 12 by Leo Davidson, 31-Aug-2017
' ********************************************************
Function OnGetCustomFields(getFieldData)
' Define checkboxes and initial states
getFieldData.fields.fOddChars = True ' Replace %20 with spaces
getFieldData.fields.fUnderscore = True ' Replace "_" with a space
getFieldData.fields.fMultiSpace = True ' Remove multiple spaces
getFieldData.fields.fExtLCase = True ' Make extension lower case
getFieldData.fields.fTitleCase = True ' Convert to true titlecase
getFieldData.fields.fParCap = True ' Capitalise after parenthesis
getFieldData.fields.fDashCap = True ' Capitalise after a dash
getFieldData.fields.fFullStopCap = True ' Capitalise after a full stop
getFieldData.fields.fFirstNonNum = True ' Capitalise first non-numeric
' Set the labels for the checkboxes
getFieldData.field_labels("fOddChars") = "Replace ""%20"" with a space"
getFieldData.field_labels("fUnderscore") = "Replace ""_"" with a space"
getFieldData.field_labels("fMultiSpace") = "Remove multiple spaces"
getFieldData.field_labels("fExtLCase") = "Make extension lower case"
getFieldData.field_labels("fTitleCase") = "Convert to Title Case"
getFieldData.field_labels("fParCap") = "Title Case: Capitalise after parenthesis"
getFieldData.field_labels("fDashCap") = "Title Case: Capitalise after a dash"
getFieldData.field_labels("fFullStopCap") = "Title Case: Capitalise after a full stop"
getFieldData.field_labels("fFirstNonNum") = "Title Case: Capitalise first non-numeric"
End Function
Function OnGetNewName(ByRef getNewNameData)
dim strExt
dim strNewName
dim iOldLength
dim iNewLength
dim strNameArray
dim strTmpWord
dim strTmpName
dim x
dim j
dim i
'Strip the filename extension for now
strNewName = getNewNameData.newname_stem_m
strExt = getNewNameData.newname_ext_m
' Make extension lower case
if getNewNameData.custom.fExtLCase then
strExt = LCase(strExt)
end if
' Replace "_" with spaces
if getNewNameData.custom.fUnderscore then
strNewName = Replace(strNewName, "_", " ")
end if
' Replace "%20" with spaces
if getNewNameData.custom.fOddChars then
strNewName = Replace(strNewName, "%20"," ")
end if
' Remove all multiple spaces
if getNewNameData.custom.fMultiSpace then
iNewLength = 0
iOldLength = len(strNewName)
Do While iOldLength > iNewLength
iOldLength = len(strNewName)
strNewName = Replace(strNewName," "," ")
iNewLength = len(strNewName)
Loop
end if
' Trim spaces at start/end of filename (e.g. because it used to start with a an underscore)
' If this is not done, the split function below may throw an error.
strNewName = Trim(strNewName)
' Convert filename to TitleCase
if getNewNameData.custom.fTitleCase then
' Create an array from words in the filename
strNameArray = split(strNewName, " ")
' Now go through each item in the array and process it
if ubound(strNameArray) > 0 then
for x = lbound(strNameArray) to ubound(strNameArray)
' Set all text to lower case to begin with
strNameArray(x) = LCase(strNameArray(x))
' Ignore articles, prepositions and conjunctions
Select Case strNameArray(x)
Case "a"
Case "an"
Case "at"
Case "and"
Case "but"
Case "by"
Case "for"
Case "in"
Case "into"
Case "is"
Case "of"
Case "off"
Case "on"
Case "onto"
Case "or"
Case "the"
Case "to"
Case "it"
Case else
if len(strNameArray(x)) > 0 then
strTmpWord = Ucase(mid(strNameArray(x),1,1)) & mid(strNameArray(x),2,len(strNameArray(x))-1)
' If the word begins with "(" then capitalise the next letter
if mid(strNameArray(x),1,1) = "(" then
if getNewNameData.custom.fParCap then
strTmpWord = Ucase(mid(strNameArray(x),1,2)) & mid(strNameArray(x),3,len(strNameArray(x))-1)
end if
end if
' If the word follows a "-" then it needs to be capitalised
if mid(strNameArray(x),1,1) = "-" then
if getNewNameData.custom.fDashCap then
strTmpWord = Ucase(mid(strNameArray(x),1,2)) & mid(strNameArray(x),3,len(strNameArray(x))-1)
end if
end if
strNameArray(x) = strTmpWord
end if
end Select
next
Else
strNameArray(0) = LCase(strNameArray(0))
End if
' Make sure the first word is capitalised
strTmpWord = Ucase(mid(strNameArray(0),1,1)) & mid(strNameArray(0),2,len(strNameArray(0))-1)
strNameArray(0) = strTmpWord
' Clear the new name variable
strNewName = ""
' Rebuild the array into one string
For x = lbound(strNameArray) To ubound(strNameArray)
strNewName = strNewName & strNameArray(x) & " "
Next
' Capitalise after - when it's in the middle of words
' Capitalise after - when it's between words
if getNewNameData.custom.fDashCap then
For j = 1 to len(strNewName)
if mid(strNewName,j,1) = "-" then
strTmpName = strTmpName & mid(strNewName,j,1)
j=j+1
if mid(strNewName,j,1) = " " then
strTmpName = strTmpName & mid(strNewName,j,1)
j=j+1
end if
strTmpName = StrTmpName & Ucase(mid(strNewName,j,1))
else
strTmpName = strTmpName & mid(strNewName,j,1)
end if
Next
strNewName = strTmpName
strTmpName = ""
end if
' Capitalise after full stops in the middle of and between words
if getNewNameData.custom.fFullStopCap then
For i = 1 To len(strNewName)
if mid(strNewName,i,1) = "." then
strTmpName = strTmpName & mid(strNewName,i,1)
i=i+1
if mid(strNewName,i,1) = " " then
strTmpName = strTmpName & mid(strNewName,i,1)
i=i+1
end if
strTmpName = StrTmpName & Ucase(mid(strNewName,i,1))
else
strTmpName = strTmpName & mid(strNewName,i,1)
end if
Next
strNewName = strTmpName
strTmpName = ""
end if
' Capitalise first non-numeric, non-space character.
' This is for music files like "07 By My Side.flac" which don't have a dash after their track numbers.
if getNewNameData.custom.fFirstNonNum then
For i = 1 to len(strNewName)
if (not IsNumeric(mid(strNewName,i,1))) and mid(strNewName,i,1) <> " " then
strNewName = mid(strNewName,1,i-1) & Ucase(mid(strNewName,i,1)) & mid(strNewName,i+1)
exit for
end if
Next
end if
end if
' Strip leading/trailing spaces
strExt = Trim(strExt)
strNewName = Trim(strNewName)
' Rejoin the name and extension and we're finished
strNewName = strNewName & strExt
OnGetNewName = strNewName
End Function