It's fairly common to want to apply several regular expressions to a filename, one after another.
While you can do this using several Rename commands in a row, that isn't ideal. Using multiple Rename commands would means you could not save the operation as a Rename Preset to use from the Rename Dialog. It is also slower as each would be renamed multiple times (once per Rename command), instead of calculating the final name of each file and renaming it once.
Instead of using multiple Rename commands, we use a rename script.
- Rename scripts in JScript, VBScript and PerlScript, to learn more about rename scripts.
- RegExp basics: Removing characters from start/end of names, to learn the basics of regular expressions.
Example rename presets
Attached are rename presets which contain the example scripts, which you can use as starting points. These are the basis for the rest of the post.
The versions of the script for Opus 12 & above are slightly better, but use features which will not work with earlier versions.
- Opus 12 & above, JScript: Multi-RegExp (O12 JS).orp (1.4 KB)
- Opus 12 & above, VBScript: Multi-RegExp (O12 VBS).orp (1.7 KB)
- Opus 11 & earlier, VBScript: Multi-RegExp.orp (1.72 KB)
- See: How to use Rename Presets from this forum
What the rename script does
Each preset has a script which does approximately the same thing, for different scripting languages or versions of Opus.
Each script, as it is, doesn't do anything very useful. The point is that it's a simple demonstration that you can adapt to do whatever you need.
Each script will do this:
- Convert the filename to lowercase. (Using the
LCasein VBScript or
- Uppercase any occurrences of the word 'ms'. (Using the respective RegExp objects in each language.)
- Uppercase any occurrences of the word 'xp'. (Ditto.)
- Convert the word "windows" to "Windows". (Ditto. Not done by the old Opus 11 version.)
The first two conversions are done case-sensitive, while the final one is case-insensitive (not that it really matters in the example, as the whole string is converted to lower-case first).
Opus 12 JScript:
Opus 12 VBScript:
RegExp syntax in JScript and VBScript
The syntax of regular expressions differs slightly between Opus and JScript/VBScript. (Sadly, there is no one standard regexp syntax and you'll find differences between almost any pair of tools.) In particular, the replacement strings use
$2, etc. in both scripting languages, instead of the
\2, etc. used in Opus.
A simple example
This thread has a simple, real-world example of a similar multi-regex script being used to solve a slightly different problem.
A more complex example
Another thread, Help needed, Spliting filenames into folders, uses a rename script which based on this one but with conditional branching to solve a more complex/esoteric problem.
Replacing multiple matches with a single expression:
Here is an aspect of regular expressions I had not noticed when first writing this example. Even in 'global' mode, a regexp will not repeatedly/recursively apply the regexp to replace multiple occurrences of something if the pattern matches the entire string.
For example, this only replaces one
%20 with a space:
re.Pattern = "(.*)%20(.*)" str = re.Replace(str, "$1 $2")
To replace all
%20 with spaces, you have to use an expression with a smaller scope, like this:
re.Pattern = "%20" str = re.Replace(str, " ")
(This is different to the special
# syntax you can add to the end of a regular expression in the Opus rename dialog. The
# at the end is an Opus-specific addition to the standard regexp syntax, and will make Opus apply the expression over and over until either the string stops changing or it starts generating a loop of results which have already been seen.)