Exclude certain folders

First, search the help docs and this forum. Maybe your question has been answered! The debugging steps can help, too. Still stuck? Delete this line and proceed.

What I’m trying to do

I have this query, which does what I want, list all my untagged files - but I want to exclude certain folders…

list where length(file.tags) = 0

Things I have tried

I’ve tried From -“My folder” and !“My folder” but that seems to mess things up and not work…

Is this for DataView?

Yes I think so, the full thing looks like this so far…

list where length(file.tags) = 0

Which I say works, but I’ve tried adding other code in that I’ve seen people suggest to exclude folders but as soon as I do it corrupts the code unfortunately.

If this is a dataview query, you need to use this syntax :

WHERE length(file.tags) = 0 AND file.path != "my folder"

Thanks for your suggestion. It doesn’t exclude the folder though :man_shrugging: Not sure why. Strange.

The solution by @Anwen matches against file.path which is the complete path of the note, i.e. My/folder/note.md. In your case you should most likely rather check against the file.folder or part of the folder name using something like startswith() or in dataviewjs .startsWith()

You should also be able to use the FROM line to exclude folders. The following query works beautifully in my context:

LIST file.etags
FROM !"ForumStuff" AND -"GithubStuff"
WHERE length(file.etags) > 0

Do note that the FROM clause needs to be in front of the WHERE clause. Or in other words, it needs to be the first after the LIST, TABLE or TASK line. And both ! and - do work. :smiley:

In my example above I exclude some folders with Stuff in their name, and I could have done this instead to exclude them:

LIST file.etags
WHERE !contains(file.folder, "Stuff")

Note that this query, and all those using WHERE to exclude stuff will initially query all files, whereas the FROM variant excludes the files not belonging to the set. So there is a slight difference in performance between these options, in addition to the logic changes.

Similar variants to match something like: ForumStuff/f75/f75705/Some note:

  • WHERE file.folder = "ForumStuff/f75/f75705" – Matches that exact folder, nothing above or below
  • WHERE startswith(file.folder, "ForumStuff/f75") – Only matching those starting with “ForumStuff/f75”, so that also include sub folders. Note: This is similar to doing FROM "ForumStuff/f75", and if not other logic says so, this should be the preferred variant
  • WHERE split(file.folder, "/")[1] = "f75" – Matches folder having “f75” as the second sub-folder. Using contains() as suggested before would match anywhere in the text, this variant locks it down to that folder part
  • WHERE endswith(file.folder, "705") – The folder needs to end with exactly “705”, so this would also match on a f56705 folder. A little whimsical example, but if you had an example as a common sub-folder, this would match all of those folders

All of these matches can be negated using !..., with exception of the first which needs to read file.folder != "...".

In summary, it’s preferable to use FROM as it removes files from the search earlier on, but WHERE on the file.folder can offer many variants on how to limit your folder search when you’re looking at parts of the folder.

Thanks for your reply holroy, and detailed explanation. I realise I’m being a bit of a idiot here, but I tried this query

LIST file.etags
FROM !"Art journal" AND -"Home/images"
WHERE length(file.etags) > 0

and it shows files with tags, not one’s without. I’m going to read through your reply in more detail and try figure out why that might be, I’m sure you’ve provided me with enough info to figure it out. Thanks

HeHe… Easily done or misunderstood, try doing WHERE !file.etags to display files without tags. (or WHERE length(file.etags) = 0 )

Thank you!
I think I got it now, using this -

LIST list where length(file.tags) = 0
WHERE !contains(file.folder, "Art journal")

I think it also automatically removes all images (I guess the ‘(file.tags) = 0’ does this job… Anyhow, that’s a precious query you’ve all helped me learn. Thanks again :+1:

1 Like

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