Listing sub-folders with dataview

Things I have tried

I wanted to create a list of folders, but I don’t understand why it’s failing.

image

Imagine this simple arrange of folders. I want a list of only the folders below “Guiones”.
So, the list should be:

  • Saga
  • Saga 2

This is the code I use:

image

I don’t fully understand it myself, and I got this result:

image

First problem I have: for some reason, “Guiones” is also part of the list. I want it gone.

Second problem: it seems that it only took 2 folders no matter what I do. I tried some irrelevant shenanigans that ended in the names changing to a folder route (“Escritos/Guiones/Saga”), but the result is the same nonetheless.

Help, my neurons are dying.

I’m using this DataviewJS in a note of the root folder:

const currentFolder = dv.current().file.folder;
const rootLength = currentFolder.length + 1; // To remove "root/"

const subfolders = app.vault.getAllLoadedFiles()
    .filter(f => f instanceof obsidian.TFolder)
    .map(f => f.path)
    .filter(path => path.startsWith(currentFolder + "/") && path !== currentFolder)
    .map(path => path.slice(rootLength))
    .sort((a, b) => a.localeCompare(b)); // Sort alphabetically

dv.list(subfolders);

It creates a list of subfolders and their subfolders. I hope it helps you play further.

Cheers, Marko :nerd_face:

I copied and pasted the code. It showed me this:

image

But I messed around with the code. In “filter path”, I changed “current folder” to the actual folder, so it ignores the first subfolder.
.filter(path => path.startsWith("Escritos/Guiones") && path !== ("Escritos/Guiones"))

image

I couldn’t cut the overall path, so, in the “root length” I added all the subfolders characters (from +1 to +9). So, this was the end code:

And result:

image

Thanks again, DiCaver. Mastermind.

1 Like

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