Browse the docs

Rename a file from a pattern

Renames a file from a pattern made of its name, its dates and a counter, keeping its extension.

Rename path to pattern

Details #

Gives the file a new name in the same folder. pattern is a template: {added:yyyy-MM-dd} {name} turns Receipt.pdf into 2026-10-29 Receipt.pdf. The extension is kept; turn keepExtension off to write the whole name, extension included.

A capture from a pattern match is {{match(file.name, "(\\d+)-(.*)", 1)}}: the first group of that regular expression. The importer writes Hazel's custom attributes this way.

Parameters #

ParameterTypeRequiredValuesWhat it does
countertextThe name of the counter {counter} uses.
intotextThe variable that receives the new path.
keepExtensionany valueAdd the original extension to the pattern. On by default.
nametext
overwritetextWhat to do when the name is taken: rename (the default; name 2.ext), replace or skip.
pathtextThe file. Optional when a file trigger fired.
patterntextThe new name, as a template. Required. name and to are other spellings.
sourcetext
totext

Every step also accepts label, onFailure, timeout and dryRunSafe.

Examples #

Rename a file from a pattern
Do
  1. Rename “Receipt.pdf” to “{modified:yyyy-MM-dd} {name}”
Open in Switchboard to try it

Gotchas #

  • A / in the result becomes -.

  • Changing only the capitals of a name works on a disk that ignores case.

  • The counter never repeats while the macro runs one file at a time, which is how a file trigger runs by default.

  • In a dry run it changes nothing and says what it would do.

Names and tokens #

Wherever a name or folder is a template, {{ }} works as everywhere, and these single-brace tokens are filled in by the step itself:

TokenGives
{name}the file name without its extension
{ext}the extension, without the dot
{fullname}the whole file name
{parent}the name of the folder it is in
{added:yyyy-MM-dd}the date the file was added to its folder (Finder's Date Added), in the format after the colon
{created:…} {modified:…} {opened:…}the creation, modification and last-opened dates
{now:…}today
{counter:3}a number that goes up by one each time this step runs, padded to 3 digits

The counter is kept in shared.hazel.counter.<counter>; counter names it (the default is this macro and step), so two steps that share a counter share the numbers. It survives restarts. Write the rest of a name with {{file.name}}, {{file.ext}} and the other file fields when a file trigger fired.

See also #

Move or sort a file, File operation, Split path