Templater - how to reference parent folder?

What I’m trying to do

Is there an easy way to reference a parent folder with Templater?

I have several work projects that I put in nested folders - Client-Project-Note. I’m using Templater to automatically create YAML frontmatter that I can reference later with Dataview.

Folder structure looks something like this:

  • Client Folder
    • Project Folder

In the note, I want to be able to extract both the Client Name and the Project Name separately so I can reference them later in Dataview.

I have no trouble accessing the immediate folder that the note is in, but how do I extract the name of the parent folder?

Things I’ve tried

I’ve seen various references to using Javascript, but that’s not really my area of expertise. I’ve tried to figure it out, but haven’t had any luck so far.

I looked at both of these:

But clearly I’m missing something about the Javascript. Can I put that inline into my Templater note? Or does the Javascript go elsewhere?
I tried installing the CustomJS plugin, but can’t get that to work.

I also tried using the file path but can’t figure out how to get just the folder name I’m looking for and not the full file path. Slice is great, but the length of the text strings is different. Unless there’s a way I can tell Templater to drop everything after the second / ?

Or is there a better way to get at this with Dataview?

I’m a sound engineer, so I try to do as much as I can, but I’m clearly not a coder… :wink:

The first response from @AlanG in https://forum.obsidian.md/t/templater-get-the-parent-folder-name/47241 should pretty much get you there.

If you have:

[folder] VaultRoot

  • [folder] Client
    • [folder] Project
      • [file] Note

And you run the following Templater template when the Note is active:

const folders = tp.file.folder(true).split('/'); //breaks the folder path into an array
const project = folders[folders.length - 1]; //gets the last element of the array
const client = folders[folders.length -2]; //gets the second to last element of the array

Then the variable project will have the value of Project and the variable client will have the value of Client.

If you want to apply these values to the note’s frontmatter, then your template should look like:

const folders = tp.file.folder(true).split('/');
const project = folders[folders.length - 1];
const client = folders[folders.length -2];
client: <% client %>
project: <% project %>

Please be aware that the above assumes you’re running the template on an empty/new note or you’ve positioned the cursor to the very beginning of an existing note.



thanks for this. I think I figured it out with some more testing. At least to get the Javascript to finally work for me. :smile:

I must admit I’m not entirely sure why it works now - I think I wasn’t putting the various scripts in the right order - but I’m happy to report it works now. Thanks!!! :slight_smile:

Much appreciated!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.