Generate list of all files containing the current file's filename

At first, I thought it would be helpful if this was a plugin that ran in the sidebar like Backlinks, but since I haven’t seen anything like that, I think I will use Dataview. HMU if you know of/know how to make a plugin like this though. :sweat_smile:

What I’m trying to do

I want to generate an automatically updating list of all files in the vault that contain the current file’s filename inside either their own filenames, or inside the file text. I’d like this to include all file types (notes, Canvas, Excalidraw, Kanban, and what not.) Basically like the search bar, but without having to type into the search bar every time.

Just in case, here’s an example. In a note called Apples.md, the code might list these other files:

  • Apples.canvas
  • Apples and oranges.md
  • Fruits.md (which contains the word “apple” in the note’s text)

I’d also like it to be usable inside a card on a Canvas (the text box) - not sure if there’s any difference for the code.

Things I have tried

I searched through the forum and found these related posts:

Maybe the Dataviewjs code in that last link can be modified for this case, but I don’t know how to write the code.

Many thanks in advance!

Try this DataviewJS query:

const currentFileName = dv.current().file.name;
const allFiles = dv.app.vault.getFiles();
const results = [];

for (const file of allFiles) {
    const fileContent = await dv.app.vault.cachedRead(file);
    if (file.name.includes(currentFileName) || fileContent.includes(currentFileName)) {
        results.push(dv.fileLink(file.path));
    }
}

dv.list(results);

Thanks for the reply. This does work in a note, besides the current file also being in the list, but this is what happens.

First, it displays this error message:

Evaluation Error: TypeError: Cannot read properties of undefined (reading 'file')
    at eval (eval at <anonymous> (plugin:dataview), <anonymous>:1:90)
    at eval (eval at <anonymous> (plugin:dataview), <anonymous>:13:4)
    at DataviewInlineApi.eval (plugin:dataview:18885:16)
    at evalInContext (plugin:dataview:18886:7)
    at asyncEvalInContext (plugin:dataview:18893:16)
    at DataviewJSRenderer.render (plugin:dataview:18922:19)
    at DataviewJSRenderer.onload (plugin:dataview:18464:14)
    at e.load (app://obsidian.md/app.js:1:1166483)
    at DataviewApi.executeJs (plugin:dataview:19465:18)
    at DataviewPlugin.dataviewjs (plugin:dataview:20386:18)

Then, the error message disappears, and it takes about 30 seconds or so for the list to appear. For some reason, there are two lists (like a duplicate) unless I switch to another tab once the error message disappears and wait for it to finish running, I come back to just one list (no duplicate).

I thought some plugins or something could be messing in it, so I tested the code in a new vault with no plugins besides Dataview, and here’s what was different:

  • The list is displayed immediately after the error message disappears (no wait time)
  • There is no duplicate list from the start

IDK what exactly is making it different in my main vault. It is a large vault though.

I also tested it on a Canvas card, and it gives me two error messages. The first message is the same as the one I pasted above, but then it changes to this:

Evaluation Error: TypeError: Cannot read properties of undefined (reading 'file')
at eval (eval at ‹anonymous> (plugin:dataview), <anonymous>: 1:90)
at eval (eval at ‹anonymous> (plugin:dataview), ‹anonymous>: 13:4)
at DataviewInlineApi.eval (plugin:dataview:18885:16)
at evalInContext (plugin:dataview:18886:7)
at asyncEvalInContext (plugin:dataview:18893:16)
at DataviewJSRenderer.render (plugin:dataview:18922:19)
at maybeRefresh (plugin:dataview:18476:18)
at HTMLDivElement.r (app://obsidian.md/enhance.js:1:11895)

And it doesn’t display anything after that.

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