How to count number of subfolders in a folder?

What I’m trying to do

I have a folder named Books underneath that, as subfolders I have a folder for each book I have/need to read/have read.

Like this:

  • Liberating Structures
  • Cooperative learning
  • etc.

Each subfolder contains all notes related to the book, being it pictures, visual notes, obsidian notes, webinar notes, etc.

I would like to count the number of Books, preferably based on metadata but I am not sure I can put meta data on a folder.

Things I have tried

I have tried this: $=dv.Pages('"Books"').length
but it gives the number of notes, not folders - can I do what I want?

Folders are used within Obsidian, but there is not any metadata related to them, and you can’t link to a folder directly. As such, your request is kind of hard to fulfill. There are two different approaches which can be taken to address this kind of issue.

Using something like dataviewjs, you can use javascript and code scripts to scan the file system manually, and present a list of the folders. However, these will not be linkable in any sense.

Another approach would be to list all notes under your Books folder, and then group these by the folder, so for example the following should list all the books with a count of how many files exists related to that folder. Note this will gladly skip folders being empty.

LIST length(rows)
FROM "Books"
GROUP BY substring(file.folder, 6)

You might want to consider a different approach though, where any given book folder has a related book note, which you then tag with some special so that you could easily query it to get all your books. You could still use your folder structure, but just add a requirement that there should be a note with the same name as the folder. There are multiple plugins, like GitHub - aidenlx/folder-note-core: Provide core features and API for folder notes in obsidian, which can keep the folder name and the “folder note” name in sync, which can be used for this purpose.

This alternate approach would also make it a lot easier to maintain metadata related to the books, as you now have a given note for each and every book, where it’s easy to add metadata.

Thank you for the elaborate response - I might make a dedicated tag for the “main” note of the book, like books/main or something like that - it should be able to do the trick :slight_smile:

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