Templater: Get the parent folder name

Hello Community,

I’m trying to use a templater script to get the parent folder name of my note as described:

To get the folder name I’m using this templater script:

function getThisProject(tp) {
    const thisFolder = tp.file.folder(false)
    if (thisFolder == "Name of folder") {
        return "Name of Folder";
    }
    return thisFolder;
}
module.exports = getThisProject;

I am currently able to retrieve the folder’s name of my note only, but not from its parent.
Any advice on how I should proceed ? (I need to tell I’m not confortable with Java)

Thank you,
François.

2 Likes
const folders = tp.file.folder(true).split('/')
const parentFolder = folders[folders.length - 2]

split('/') will break the path up into an array of individual folders using, by splitting on the / character.

[folders.length - 2] will get the 2nd-to-last element (e.g. the parent folder)

1 Like

Thanks for your prompt answer @AlanG

I’ve updated the script with the following code:

function getThisProject(tp) {
    //const thisFolder = tp.file.folder(false)
    const folders = tp.file.folder(true).split('/')
    const parentFolder = folders.slice(-2, 1)
    //if (parentFolder == "Name of folder") {
    //    return "Name of Folder";
    //}
    return parentFolder;
}
module.exports = getThisProject;

When I am creating a new note, my “projectName: <% tp.user.getThisProject(tp) %>” value is replace by something empty.

Is there any way to troubleshoot the script execution ?
Any ideas of what’s going wrong ?

Thank you :slight_smile:

No idea sorry, it works perfectly for me exactly as you’ve written it there.

Here’s a perfectly working demo, using your exact code above:

https://drive.google.com/file/d/1u7Q5VXC-SJLsUe7s2OCvoGGLIxYk5BT_/view?usp=sharing

1 Like

Maybe because I am on Mac OS ? :thinking:

Did my demo vault work, if you launch the template from the note in the “Child” folder?

There shouldn’t be any difference on Mac.

I’ve tried and unfortunately it does not work, same behavior.

I think I found the issue. It seems to be due to the slice.
Here is the updated code:

function getThisProject(tp) {
    const folders = tp.file.folder(true).split('/')
    const parentFolder = folders.slice(1, 2)
    return parentFolder;
}
module.exports = getThisProject;
1 Like

That will give you the 2nd from the left folder name, which won’t work if you create a deeper child folder.

In my post I had previously edited to have this code:

const parentFolder = folders[folders.length - 2]

Maybe give that a try?

1 Like

Is works as expected.
thanks a lot for your help

I just want to clarify why your slice approach didn’t work, you were almost there: -2, 1 are not valid values in slice(-2,1) - slice(n1, n2) takes a ‘slice’ from index n1 to n2 using negative values for counting from the end of the array. So you were right about using -2, however you should also use -1 as the second parameter, that would give you the element 2nd to last. But slice() always returns an array so you would have to extract the first element of that array, like so: slice(-2, -1)[0] - or simply slice(-2)[0].

This is breaking my brain. I tested your vault AlanG and it works - but when using the same setup on my own vault I get an error

Templater Error: Template parsing error, aborting. 
 await is only valid in async functions and the top level bodies of modules

From what I can see are all settings the same, the function is the same - what did I miss?

Hmm, I can’t delete or edit my post.
But I think I found the issue, another user script was somehow messing with it. A bit odd

Same thing happened to me.
Had to remove a js file from the folder common with templater js files and then my default template worked…
Saved me some time here buddy.

1 Like