Count Files in Folder using Dataview

What I’m trying to do

Using the dataview and templator plugins, I’m trying to display the number of notes inside the current folder.
Essentially, I have a folder of lecture notes for each class and I want a line at the top of each note that says “Lecture 5 of x” with x being the number of notes in the current folder.

Things I have tried

I have a template setup which works with the this dvjs query below:


The problem being that the ‘myclass’ part needs to be a dv function to get the path of the current file’s parent folder since it is a template. I can’t seem to get the syntax correct in order to pass this function to get the current folder path to the pages function.
I’ve tried differing variations of this but can’t seem to get it to work:

`$= dv.pages('"dv.current().file.folder"').length`

Any help is appreciated, I assume this is a silly syntax thing that I’m missing.

Thanks in advance

1 Like

For starters you could try with:

`$= dv.pages(`"${ dv.current().file.folder }"`).length`

But I’m not sure this is the optimal way to get that number. It might be better to look into the various cache structures of the apps metadata.

Update: Backticks within an inline query like this don’t work, so this could have been used within a full dataviewjs query. Even though I still think the cache structure would be better suited for this purpose.

1 Like

Don’t know why, but I can’t get the code you suggested to work—throws a syntax error:

`$= dv.pages(`"${ dv.current().file.folder }"`).length`

(With apologies to the OP for this hijack, I want to thank you for the help with the game-tracking code. I have been offline for some time so couldn’t reply sooner, and I can’t reply on that thread as it is now locked.)

My bad, you can’t use backticks like that in inline queries. Try

`$= dv.pages('"' + dv.current().file.folder + '"').length`

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