Code for Inline Base to list all PDFs in a folder (including sub-folders)

What I’m trying to do

I’m trying to write the code for an inline Base to list all the Pdf files that are in the Parent folder and all its sub-folders.

My usual folder structure is like this

And I try to always put my Pdf documents into the PDF folder, but sometimes I forget and they appear as below:

and the Pdf file isn’t in the correct folder.

I have learned how to do this by using a view of a Base, by filtering for the file extension Pdf.
Using a Base will also return the Misplaced Pdf file.

I’ve been trying to learn how to embed a base as a code block. I have read the help files here. I’m not at a level where I can understand enough about coding queries yet, but I have tried!

Things I have tried

I found this code which created a table which showed all the files that links into the open file.

This is the code and I found it on this Reddit thread here.

```base
views:
  - type: table
    name: Table
    filters:
      and:
        - file.hasLink(this.file.name)
    order:
      - file.name
      - file.folder
      - file.ctime
      - file.mtime
    columnSize:
      file.name: 500
      file.folder: 500
      file.ctime: 175
      file.mtime: 175

And I tried to use bits of the above code and the code shown in the Obsidian Help page to create my own Inline Base.

This is the code I wrote - but it doesn’t work!

views:
  - type: table
    name: Table
filters:
  and:
   - file.ext(pdf)
  order:
      - file.name
      - file.folder
      - file.ctime
 columnSize:
      file.name: 500
      file.folder: 500
      file.ctime: 175
      file.mtime: 175

The error says “encountered an error when rendering the code block”.

I tried swapping the line

-File.ext(pdf) for

-file.inFolder(“This.Folder”)

But I still got an error!

If I could code this correctly I would save it as a template and then insert the template as an inline code block and see all the pdfs in that parent folder, even if they are not in the correct location.

Any advice appreciated :grinning_face:

.

1 Like

Any reason why you can’t just create a basic code for base and adjust the settings via the UI?

By that I mean just create a code like this,

```base
```

Which will create a ‘raw’ base view you can tweak, then copy the code for the template

That should also point out the correct way of writing the codes too.
That’s how I found out that the filter for pdf should be

filters:
  and:
   - file.ext == "pdf"

Thanks @dmantisk,

I did what you suggested and it worked! Thanks.

Now, I have made a template and this is the code I used:

filters:
  and:
    - file.inFolder("<% tp.file.folder(true) %>")
views:
  - type: table
    name: Table
    filters:
      and:
        - file.ext == "pdf"

I enclosed that code inside:

image

Now, when I insert the template into a page, I have to run the Templater code by pressing:

Alt + R

and the Base query returns all the PDFs in the folder!

And I can play around with the queries and copy the inline code too!

My next plan is to create a code block query to list the .md files in a folder and show the outgoing file links property which excludes .png images that I have pasted into the file.

The method you suggested will help me with this!

Thanks again :+1:

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