Newbie: Create folders in a path based on dates

I’m trying to create folder based on a date but in folders also based on dates, so here is the example:


So ideally it would then create

But I’m guessing just invoking that first path as a Create a folder Macro isn’t enough. And second time the folders 2016/01/ would exist.

Be gentle this is a first script! :smile:

You could use a bunch of New Folder actions, one for each sub directory, and each configured to ignore any errors.


  • New Folder ~/Pictures/%ICUDateTime%yyyy%
  • New Folder ~/Pictures/%ICUDateTime%yyyy%/%ICUDateTime%MM%
  • New Folder ~/Pictures/%ICUDateTime%yyyy%/%ICUDateTime%MM%/%ICUDateTime%yyyyMMd%

Alternatively, you probably want to use the folder path for something later in the macro, so put it in a variable, and then use mkdir -p, ie:

  • Set Variable Path to ~/Pictures/%ICUDateTime%yyyy%/%ICUDateTime%MM%/%ICUDateTime%yyyyMMd%
  • Execute Shell Script: mkdir -p “$KMVAR_Path”

Hey Richard,

Ha – Peter beat me to the punch...

This is simple, but it requires a little special knowledge.

The problem you're running into is that a string of directories cannot be created by Keyboard Maestro in one fell swoop.

If the intermediate directories do not exist then Keyboard Maestro will error – e.g. only the LAST directory in the chain can be created – and only if ALL the preceding directories already exist.

The trick to the task is to use the shell's mkdir command which can create intermediate directories (using the -p switch).

We could do this entire task in the shell, but let's use Keyboard Maestro functions for as much of it as we can.

First saving your $HOME-based (tilde) directory string to a variable.

Then expanding the tilde using a filter, which makes it easier to use in the following shell script.

Then a simple shell script that uses a Keyboard Maestro variable.


Here's the shell script one-liner:

mkdir -p $HOME/Pictures/$(date "+%Y/%m/%Y%m%d")

Ah, I wasn't paying attention when I cut-and-pasted the path string. You only need to employ ICUDateTime once:


* AppleScript-Objective-C is also capable of making intermediate directories, although vanilla AppleScript cannot.


Set Variable var to Text.kmmacros (2.5 KB)

1 Like

Cool, thanks for the response guys - I will give it a go! :smile:

Worked a treat, thank you. And best of all, I understand more!