How to only show "Subfolder" as opposed to entire path in Dataview query

How do I only display the final “Sub-Folder” in a dataview query. Below is how far I’ve managed to get…

TABLE rows.file.link as "Statute"
FROM "⚖️ Law/Statute/Personal Property Security Act, RSO 1990, c P.10"
WHERE file.name != this.file.name
GROUP BY file.folder AS Section

Produces…

But if I’d like to have only the final subfolder appear in the left-hand column - how do I go about doing that?

Any help is appreciated,
Cheers!

I’d love a better solution but the ‘bodge’ that I use is to use the substring function to strip off the front part of the path.

e.g.

substring(file.folder,12)

I think that 12 would be the right number for your example of: Law/Statue/

But I look forward to a better, more adaptable answer!

EDIT:
You’d need a higher number due to the icon which I didn’t see at first. Try 14 first and have a play.

If not flawed, the following at least works in local tests:

GROUP BY regexreplace(file.folder, ".*\/([^\/]+)$", "$1") AS Section

Test environment: regex101

So, in full:

```dataview
TABLE rows.file.link as "Statute"
FROM "⚖️ Law/Statute/Personal Property Security Act, RSO 1990, c P.10"
WHERE file.name != this.file.name
GROUP BY regexreplace(file.folder, ".*\/([^\/]+)$", "$1") AS Section
```

:crossed_fingers:


1 Like

Two very minor comments on this regex:

  1. You’re requiring the / to be present for the regex to kick in, which could have mean’t that first level folders wouldn’t match. But in this case, it’s just returning the unchanged file.folder so no problem.
  2. At regex101.com, you’ve selected to use PCRE regex style (which is for PHP regex), I believe Obsidian is using the ECMAScript variant. Still a minor issue for this regex, but for more complex variants that could make a difference

Other than those very minor thingies, it sure seems to do the trick! :smiley:

2 Likes

Works perfectly - Thank you!

1 Like

Glad it works, @NPop

Thanks for the tips, @holroy

Changed the flavour as suggested, and it seems to work.

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