Skip to main content
The TFolder class represents a folder in the vault. It extends TAbstractFile and provides methods and properties specific to folders.

Inheritance

TFolder extends TAbstractFile.

Properties

Inherited from TAbstractFile

vault
Vault
Reference to the vault this folder belongs to.
path
string
Full vault-relative path of the folder (e.g., "folder/subfolder").
name
string
Name of the folder (e.g., "subfolder").
parent
TFolder | null
The parent folder of this folder, or null if this is the root.

TFolder Properties

children
TAbstractFile[]
Array of child files and folders contained in this folder.

Methods

isRoot

isRoot(): boolean
Check if this folder is the root folder of the vault. Returns: true if this is the root folder, false otherwise. Since: 0.9.7

Usage

The TFolder class is typically returned by various Vault methods and is not directly instantiated. You can use instanceof to check if an abstract file is a folder:
const abstractFile = this.app.vault.getAbstractFileByPath('MyFolder');

if (abstractFile instanceof TFolder) {
  // This is a folder
  console.log('Child count:', abstractFile.children.length);
}

Examples

Accessing folder properties

const folder = this.app.vault.getFolderByPath('Projects/Work');

if (folder) {
  console.log('Folder path:', folder.path);        // "Projects/Work"
  console.log('Folder name:', folder.name);        // "Work"
  console.log('Parent folder:', folder.parent?.path); // "Projects"
  console.log('Is root?', folder.isRoot());        // false
  console.log('Children count:', folder.children.length);
}

Listing folder contents

const folder = this.app.vault.getFolderByPath('MyFolder');

if (folder) {
  console.log('Contents of', folder.name + ':');
  
  for (const child of folder.children) {
    if (child instanceof TFile) {
      console.log('  📄', child.name);
    } else if (child instanceof TFolder) {
      console.log('  📁', child.name);
    }
  }
}

Getting files in a folder

import { TFile, TFolder } from 'obsidian';

const folder = this.app.vault.getFolderByPath('MyFolder');

if (folder) {
  // Get only files (not subfolders)
  const files = folder.children.filter((child): child is TFile => 
    child instanceof TFile
  );
  
  console.log(`Found ${files.length} files`);
  
  // Get only markdown files
  const mdFiles = files.filter(file => file.extension === 'md');
  console.log(`Found ${mdFiles.length} markdown files`);
}

Getting subfolders

import { TFolder } from 'obsidian';

const folder = this.app.vault.getFolderByPath('MyFolder');

if (folder) {
  // Get only subfolders
  const subfolders = folder.children.filter((child): child is TFolder => 
    child instanceof TFolder
  );
  
  console.log(`Found ${subfolders.length} subfolders`);
  
  for (const subfolder of subfolders) {
    console.log('Subfolder:', subfolder.path);
  }
}

Recursively processing all files in a folder

import { TFile, TFolder, Vault } from 'obsidian';

const folder = this.app.vault.getFolderByPath('MyFolder');

if (folder) {
  const allFiles: TFile[] = [];
  
  Vault.recurseChildren(folder, (file) => {
    if (file instanceof TFile) {
      allFiles.push(file);
    }
  });
  
  console.log(`Total files in folder tree: ${allFiles.length}`);
}

Working with the root folder

const root = this.app.vault.getRoot();

console.log('Root path:', root.path);     // ""
console.log('Is root?', root.isRoot());   // true
console.log('Root name:', root.name);     // "/"

// Get all top-level items
for (const child of root.children) {
  console.log('Top-level item:', child.name);
}

Creating a nested folder structure

// Create nested folders
try {
  const folder = await this.app.vault.createFolder('Projects/2024/Q1');
  console.log('Created folder:', folder.path);
} catch (error) {
  console.error('Failed to create folder:', error);
}

Counting files by type in a folder

import { TFile, TFolder, Vault } from 'obsidian';

const folder = this.app.vault.getFolderByPath('MyFolder');

if (folder) {
  const filesByExtension = new Map<string, number>();
  
  Vault.recurseChildren(folder, (file) => {
    if (file instanceof TFile) {
      const count = filesByExtension.get(file.extension) || 0;
      filesByExtension.set(file.extension, count + 1);
    }
  });
  
  console.log('Files by extension:');
  filesByExtension.forEach((count, ext) => {
    console.log(`  .${ext}: ${count} files`);
  });
}

Checking if a folder is empty

const folder = this.app.vault.getFolderByPath('MyFolder');

if (folder) {
  const isEmpty = folder.children.length === 0;
  console.log('Folder is empty:', isEmpty);
  
  // Check if folder has any files (not just subfolders)
  const hasFiles = folder.children.some(child => child instanceof TFile);
  console.log('Folder has files:', hasFiles);
}
  • Vault - Main class for working with folders
  • TFile - Represents a file in the vault
  • FileManager - Higher-level folder operations

Build docs developers (and LLMs) love