Dataviewjs list files inside current md folder

Things I have tried

What I’m trying to do

Hi. I’ve a folder workflow. Every folder has a *.md file inside with the same name as folder.


I list all files (including images, pdf, etc) that are in folder with this dataview search:

const pdfFiles = app.vault.getFiles().filter(file => file.path.includes(dv.current().file.folder))

dv.list( => dv.fileLink(file.path)))

I have 2 questions:

  1. It lists even subfolders, how could I list only “” level files.
  2. Where do I have to put this code to enable this search on all *.md files (avoiding to write it every time I create a *.md)

Thank you

In the case of /folder/subfolder/ you’ll get loads of metadata, including these two:

file.path = /folder/subfolder/
file.folder = /folder/subfolder

So the answer to your first question is to change from a file.path.includes(...) to an equality check, like file.folder == dv.current().file.folder

I don’t quite get what you want out of your second question, could you examplify it some more?

Would the it answer your question, to enter the generic code into a dv.view() script, like if you added the following code into vault/js/listAllFiles.js:

const pdfFiles = app.vault.getFiles().filter(file => file.folder == dv.current().file.folder))

dv.list( => dv.fileLink(file.path)))

You could then in each of your folder-index files, write:

`$= dv.view('js/listAllFiles') `

Hi, thank you for your response. I’ve tried to write this (deleting “dv.current().file.folder))” last parenthesis) but it doesn’t show anything

second question answer is correct, but without having to write the code on every page.

if file name = folder name, list folder files

I misread parts of your request, and file.folder is not available in your current context. What’s available is file.parent.path, and that is the folder of the current file. Some further testing indicates that there is a slight difference when handling the root folder as the file.parent.path then becomes /, but the dv.current().file.folder becomes empty.

With that taken into account try this one to list all files in the current directory:

let parentFolder = dv.current().file.folder

if (parentFolder == "")
  parentFolder = "/"
const lsFolder = app.vault.getFiles()
  .filter(file => file.parent.path == parentFolder )
  .map(file => dv.fileLink(file.path))


thank you, that is what I needed!

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