Is there a way to trim a file.folder path and apply a link to it?

To the best of my ability, I’ve read documentation, searched forums, etc., but I’m stuck. Also, I am no programmer, by any stretch of the imagination, so please assume I’m an idiot with code because that would be correct. :joy:

What I’m trying to do

I’ve got a vault of my novel writing (multiple novels) and would like to keep an automated TOC for each book.

What I would LIKE is this:

  • Folder Name 01 (linking to first item in its folder)
    – Linked note 01
    – Linked note 02
    – Linked note etc
  • Folder Name 02 (linking to the first item in its folder)
    – Linked note 01
    – Linked note etc

I’ve managed to make a primitive version of the above, but it’s not as elegant as I’d like.

Things I have tried

My current code:

LIST rows.file.link
FROM "My/Very/Nested/Folder"
GROUP BY file.folder 

This gives me:

  • Unlinked and Overly Long Folder Path 01
    – Linked note 01
    – Linked note 02
    – Linked note etc
  • Unlinked and Overly Long Folder Path 02
    – Linked note 01
    – Linked note etc

It’s automatic (hooray!), and the most important stuff works, but I’d really like to trim the file.folder path. Right now, the path is obnoxiously long because of my structure, delivering winning titles like Novels/Series Name/Book Name/Chapter Name, when I’d really just like it to use Chapter Name from the path; the linked notes are scenes from the chapters.

A link to the first note in the Chapter Name folders would also be nice but is not necessary, if I’m honest. In a way, I’m just curious if it can be done.

Try this:

```dataview 
LIST 
	rows.file.link
FROM 
	"/"
FLATTEN 
	regexreplace(file.folder, ".*\/([^\/]+)$", "$1") AS F
GROUP BY 
	F

Here’s the solution:

Here’s another. Please try this one:

```dataview 
LIST 
	rows.file.link
FROM 
	"/"
GROUP BY 
	link(file.folder)```

There is an option to change the GROUP BY ... statement into producing links, but it’ll be hard to make it link to the first file. If however, you used something like the Folder notes plugin, aka having a file in the folder having the same name as folder, or a given fixed name, it can be done using an ordinary query.

If you’re willing to switch over to dataviewjs, it would also be possible to do this using a TABLE query which holds the folder name in the first column, and a list of the file in the second column. This list could then be traversed and one could produce the output you want through tracking whenever the folder name changes, or something similar to that.


Regarding how to get the last sub-folder you could use the regexreplace() as suggested in other threads (by me(?) originally), which I believe was used in another context to split and match against parts of the folder. Another similar option, if you only want the last folder, is to do something like: reverse(split( file.folder, "/" ))[0]

@sanyaissues Both of your snippets cleaned everything up! I would have never come up with those solutions myself, haha, so thank you. I’ll stick with your first clean but non-linked solution; maybe your second solution will work for me if Obsidian ever does things at the folder level. Like a lot of writers I’ve seen on the forum here, I’d love for the app to temporarily concatenate the notes within a folder for viewing/editing, a la Scrivener, but I’ll be shocked if that feature ever lands.

@holroy Thanks for your thoughts on this! I have looked at Folder Notes, but it’s not exactly what I need, and I worry about future backwards compatibility or messiness with new features. I’m trying to run my vault with as few plugins as possible.

1 Like

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