DataView: List Files/Folders but not recursively

Assume this file/folder structure:

  • DemoFolder:
      1. Welcome.md
      1. PrivacyPolicy
      • PrivacyPolicy.md
      1. AboutUs.md
      1. WorkingHours
        1. WorkingHours.md
      • 4.1 OtherNote1.md
      • 4.2 OtherNote2.md

NOTE:

  • I am using a folder-note plugin, but I want the file listing to be done purely with dataview.

What I want:

  • I want to list all the files/folders that are in DemoFolder, but not listing them recursively
    • ie: exclude any files that belong to any subfolders
  • I want my query to only include:
    • 1. Welcome.md
    • 2. Privacy Policy (folder)
    • 3. AboutUs.md
    • 4. WorkingHours (folder)

Things I have tried

Query 1:

  • This lists everything (ie include notes in subfolders)
LIST
WHERE startswith(file.folder, this.file.folder)

Query 2:

  • this excludes any folders in DemoFolder altogether
  • it also includes the DemoFolder itself (which I do not want)
LIST
WHERE endswith(file.folder, this.file.folder)
1 Like

alx-folder-note specific solution

  • currently the plugin does not list all files, folder-notes of a current folder

Solution:

  • I was able to do it using dataviewjs
  • took inspiration from other threads in this forum
let parentFolder = dv.current().file.folder

if (parentFolder == "")
  parentFolder = "/"

const lsFiles = app.vault.getFiles()
  // file or folder-note in cur folder
  .filter(
  // a file in cur folder
  file => file.parent.path == parentFolder
  || // or a folder-note in parent folder
  (file.parent.parent.path == parentFolder
   && file.name == file.parent.name +".md")
  )
  // exclude cur folder-note itself
  .filter(file =>
	  file.name != parentFolder.split("/").at(-1)+".md")
  .map(file => dv.fileLink(file.path))
  .sort()


dv.list(lsFiles)
1 Like

Doing a query to list files in the current folder is easily achieved by doing:
WHERE file.folder = this.file.folder, given that the query is in the same folder.

However, I’ve not seen any dataview query capable of listing just the folder by itself (without references to any notes). It can be done if you use dataviewjs and stuff like app.vault.getFiles or other means of file traversal, but I think most of the queries are related to the actual notes (having a file folder/path), and not the actual file folders (if that make sense).

What do you want to use the listing for? Do you want it to link somewhere, or is it just for visual purposes?

2 Likes

The purpose is to create a Table of Contents kind of thing.
My answer works, but I’m having issues on iOS devices…

Solution

  • Is to use CustomJS plugin
  • The code will run correctly on mobile devices too
    • loading with require did not work on iOS (and maybe on mobile in general)

1. CustomJS File:

  • install plugin, and load code like below
class DvFiles {
    list(app, dv) {
       
      let parentFolder = dv.current().file.folder

      if (parentFolder == "") parentFolder = "/"

      const lsFiles = app.vault.getFiles()
        // file or folder-note in cur folder
        .filter(
        // a file in cur folder
        file => file.parent.path == parentFolder
        || // or a folder-note in parent folder
        ( file.parent.parent != null // not at root
          && file.parent.parent.path == parentFolder
         && file.name == file.parent.name +".md")
        )
        // exclude cur folder-note itself
        .filter(file =>
          file.name != parentFolder.split("/").at(-1)+".md")
        .map(file => dv.fileLink(file.path))
        .sort()

        dv.list(lsFiles)
    }
}

2. Load and use the script

const {DvFiles} = customJS
DvFiles.list(app, dv)
2 Likes

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