Listing PDFs using Dataview

Things I have tried

I found a post here that talks about a possible way to find PDFs using DataviewJS, but I couldn’t get it to work.

What I’m trying to do

Basically I’m scanning in my son’s homework from Kindergarten and placing them in a folder called “Scans” in his “Kindergarten” folder. I have a note called Kindergarten+ (I add plusses to all my dashboards like I’m a streaming service for some reason, lol) and I’ve been manually linking to his scans.

I’d rather just run a quick dataview code that can just list everything from his Scans folder by created time.

Anyone got a way to do that?

What doesn’t work in particular? Filter the folder?

Can you give some more information how exactly your (failed) code looks like?

Besides that, if all pdfs reside in just one folder and this folder only contains the files you want to list, some simple query like this should probably work:

TABLE file.ctime AS created
FROM "Scans"
SORT file.ctime DESC

The point is: Dataview works only with md files. :slight_smile:
(ignoring the particular case of csv files)

Then my solution probably shouldn’t work… :laughing:

Actually, I haven’t thought about this as I never needed to list non-md files myself… :wink:

Sure, here’s what my code looks like:

```dataviewjs
const pdfFiles = app.vault.getFiles().filter(file => file.extension === 'pdf' && file.path.includes('11 - 👦🏻 Finn/Kindergarten/Scans')) dv.list(pdfFiles.map(file => dv.fileLink(file.path)))

And here’s what I get back:

Evaluation Error: SyntaxError: Unexpected identifier
    at DataviewInlineApi.eval (plugin:dataview:19669:21)
    at evalInContext (plugin:dataview:19670:7)
    at asyncEvalInContext (plugin:dataview:19680:32)
    at DataviewJSRenderer.render (plugin:dataview:19701:19)
    at DataviewJSRenderer.onload (plugin:dataview:19285:14)
    at DataviewJSRenderer.e.load (app://obsidian.md/app.js:1:852538)
    at t.e.addChild (app://obsidian.md/app.js:1:852939)
    at t.addChild (app://obsidian.md/app.js:1:1242194)
    at Object.addChild (app://obsidian.md/app.js:1:1241219)
    at DataviewApi.executeJs (plugin:dataview:20210:19)
  1. I’m still using the public version of Obsidian, not the insider one (v.016). Because that I don’t know if there’s any changes with app.vault.getFiles()
  2. About the syntax error, you can try without the emoji? You don’t need to use the full folder path. Try just file.path.includes('Kindergarten/Scans')

Another thing:

  • if you want only one line you need to separate the different parts of the code with the ;
const pdfFiles = app.vault.getFiles().filter(file => file.extension === 'pdf' && file.path.includes('11 - 👦🏻 Finn/Kindergarten/Scans')); dv.list(pdfFiles.map(file => dv.fileLink(file.path)))
  • if not, you need at least to break the line:
const pdfFiles = app.vault.getFiles().filter(file => file.extension === 'pdf' && file.path.includes('11 - 👦🏻 Finn/Kindergarten/Scans'))
dv.list(pdfFiles.map(file => dv.fileLink(file.path)))

(I’m not versed in JS, but I guess it’s something like what I said :slight_smile:)

2 Likes

More problematic will be the sort thing… because you can’t use the file.ctime but stat.ctime inside app.vault.getFiles()
And play with dates inside JS is (at least for me, a js dumb) a painful thing.

So it literally worked by adding the line break. I thought it was in there, but it was just a wrap. I added the line break and it worked.

What is the dataviewjs command for sorting it by created time?

Oh I just saw your other comment about the file.ctime. I’ll check and see how to do that.

1 Like

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