v1.9.356.0 introduces a generalization of the regular expressions that can now be applied to any variable, and a new {FilePath[n]} option allowing to split a path into its components as well as a number of string manipulation functions that can all be composed together.
Let's start from the new option for path components: some users have expressed the need to parse the path (not just the filename) to extract the name of the parent or grandparent folder, for example, and use it / them as component of the new file names they are creating.
The RegEx* macro was originally introduced to that effect (see above) and made it possible to split and capture path components, for example:
c:\images\birds\ducks\img1.jpg can be renamed to
birds_ducks_img1.jpg using for example:
- Code: Select all
{RegEx*:.*\\(.*)\\(.*)\\.*}{1}_{2}_{FileName}
So far so good but the expression is a bit cryptic, so v1.9.356 introduced the simpler to use {FilePath[n]} option, where n is between 0 and 9, and where 0 means the file name part, 1 the parent folder, 2 the grandparent folder etc, so you can reach up to 8 levels above the current folder you are in.
You get the same result as the above {RegEx*} with:
- Code: Select all
{FilePath[2]}_{FilePath[1]}_{FileName}
which is a lot easier to read. Note that {FileName} is now equivalent to {FilePath[0]}, both can be used interchangeably.
Version 1.9.356 also generalizes the notion of regular expressions by allowing to apply an expression to any variable. With the new {RegEx{<variable>}} syntax, the {RegEx*} example above can be rewritten as:
- Code: Select all
{RegEx{FilePath}:.*\\(.*)\\(.*)\\.*}{1}_{2}_{FileName}
The syntax is thus {RegEx{<variableName>}:<expression>} and you can of course combine everything as for example in:
- Code: Select all
{RegEx{FilePath[2]}:.*(.{4})}The last 4 characters of the grandparent folder are '{1}'
Note that any variable can be used in the new RegEx{} mechanism, opening the door for very sophisticated pattern extraction and capture from any of the hundred+ existing substitution variables.
This is just an example of regular expression. You can achieve the same effect with the simpler {Right{},n} function as below:
- Code: Select all
The last 4 characters of the grandparent folder are '{Right{FilePath[2]},4}'
The {RegEx*} variable will continue to work exactly as before for backwards compatibility with existing templates, but this syntax is deprecated: use the new {RegEx{FilePath}} syntax in new templates as it is more readable.
One more example:
- Code: Select all
{RegEx{FileHash:1}:(.{8}).*}The first 8 characters of the file content's SHA-1 hash are '{1}'
Remember that you can access a context menu listing all the available substitution variables by right-clicking in the template editors.
Again, this is just another example of regular expression. You can achieve the same effect with the simpler {Left{},n} function as below:
- Code: Select all
The first 8 characters of the file content's SHA-1 hash are '{Left{FileHash:1},8}'
The string manipulation functions that have been added are summarized below:
{LTrim{variable}}
{RTrim{variable}}
{LRTrim{variable}}
{InnerTrim{variable}}
{AllTrim{variable}}
{SingleSpace{variable}}
{Left{variable},length}
{Right{variable},length}
{Mid{FileName},start,length}
{Upper{variable}}
{Lower{variable}}
{TitleCase{variable}}
{Reverse{variable}}
{Replace{variable},"search_regex","replacement"}
Functions and macros are very similar and can be composed, for example consider the following:
- Code: Select all
{InnerTrim{TitleCase{Replace{FileName},"_"," "}}}
What's happening here:
- First, the underscores are replaced by spaces
- Next, each word's first letter is capitalized
- Finally, the inner spaces are removed
This expression transforms "an_example_of_cool_name_change" into "AnExampleOfCoolNameChange" - isn't that cool?