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.

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)

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

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].