Beginner Question: How can I exclude a folder from a given dataviewjs query?

I was not able to find a simple answer for non programmer to solve my problem. I like to exclude a folder (in my case /readwise and his subfolders) in a given query. What would be the code for that?

Specific:
I use the follow query out of ideaverse pro to see placeholder notes with at least 3 links pointing to it. But I can’t use the workflow cause it shows to much results that I don’t need because of a folder that should’nt be included. The code is the follow, but what do I have to implement, for leaving the readwise out of the results?

[!orbit]- #### Accreting placeholder notes with at least 3 links pointing to it
If you get the spark, turn the placeholder note into an actual note.

const count = 3;
let d = {};
function process(k, v) {
  Object.keys(v).forEach(function (x) {
    x = dv.fileLink(x);
    if (d[x]==undefined) { d[x] = []; }
    d[x].push(dv.fileLink(k));
  });
}

Object.entries(dv.app.metadataCache.unresolvedLinks)
    .filter(([k,v])=>Object.keys(v).length)
    .forEach(([k,v]) => process(k, v));
    
dv.table(["Accreting placeholder notes", "Linked from"],
         Object.entries(d)
         .filter(([k, v]) => v.length >= count)
         .sort((a, b) => b[1].length - a[1].length)
         .map(([k,v])=> [k, v.join(" • ")]))

You can also think of these as: shadow seeds, secret slowburns, accreting placeholder notes

Anybody able to help? That would be great :slight_smile:

the easiest way to skip folders is to use sources to exclude. One I use all the time is !"templates" for example.
https://blacksmithgu.github.io/obsidian-dataview/reference/sources/

The issue is if you’re using dataviewjs, your options for doing this is with dv.pages() or one of the execute or query functions, where it looks like you’re just using fileLink and a big list of unlinked files here.
https://blacksmithgu.github.io/obsidian-dataview/api/code-reference/#dvpagessource

One way to hack this in without interfering too much with how your code works now would be to use dv.pages() to get a list of all pages that COULD be valid (ie, excluding that pesky folder) then checking all your fileLink paths against that list to see if you should proceed, but it feel a bit cart/horsey.

1 Like

To exclude folder we got some options like @cheezopath says:

  • In a DQL query you do: FROM !"my/folder"
  • In dataviewjs you could do: dv.pages('!"my/folder"')
  • And in your script it’s a little bit more tricky :smiley:, but we need to filter on the path using something like !path.startsWith("my/folder")

When it’s calling process(k, v) the k parameter is the full path to the note which links to the unresolved note, so this is one place we could check to exclude the folder. So if you replace the process(k, v) function with this:

function process(k, v) {
  Object.keys(v).forEach(function (x) {
    x = dv.fileLink(x);
    if ( !k.startsWith("my/folder") ) {
      if (d[x]==undefined) { d[x] = []; }
      d[x].push(dv.fileLink(k));
    }
  });
}

The function above would exclude the my/folder from this particular search. This exclusion could also be done before we call the process(k, v), and then you’d replace the code below the function with this section (and you could keep the function as it was before):

Object.entries(dv.app.metadataCache.unresolvedLinks)
    .filter(([k, v]) => !k.startsWith("my/folder"))
    .filter(([k, v])=>Object.keys(v).length)
    .forEach(([k, v]) => process(k, v));

This could be slightly more efficient as we ignore the folder on an earlier stage.

2 Likes

Thank you very much you booth for your kind answers. This helped!

1 Like

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