The TFile class represents a file in the vault. It extends TAbstractFile and provides additional properties specific to files.
Inheritance
TFile extends TAbstractFile.
Properties
Inherited from TAbstractFile
Reference to the vault this file belongs to.
Full vault-relative path of the file, including the extension (e.g., "folder/note.md").
Name of the file, including extension (e.g., "note.md").
The parent folder of this file, or null if this is the root.
TFile Properties
File statistics including creation time, modification time, and size.interface FileStats {
ctime: number; // Time of creation (unix timestamp in milliseconds)
mtime: number; // Time of last modification (unix timestamp in milliseconds)
size: number; // Size on disk in bytes
}
Name of the file without the extension (e.g., "note" for "note.md").
File extension (e.g., "md", "pdf", "png").
Usage
The TFile 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 file:
const abstractFile = this.app.vault.getAbstractFileByPath('MyNote.md');
if (abstractFile instanceof TFile) {
// This is a file
console.log('File extension:', abstractFile.extension);
console.log('File size:', abstractFile.stat.size);
}
Examples
Accessing file properties
const file = this.app.vault.getFileByPath('folder/MyNote.md');
if (file) {
console.log('Full path:', file.path); // "folder/MyNote.md"
console.log('File name:', file.name); // "MyNote.md"
console.log('Base name:', file.basename); // "MyNote"
console.log('Extension:', file.extension); // "md"
console.log('Parent folder:', file.parent?.path); // "folder"
console.log('File size:', file.stat.size); // Size in bytes
console.log('Modified:', new Date(file.stat.mtime));
}
Checking file type
const file = this.app.vault.getFileByPath('document.pdf');
if (file) {
if (file.extension === 'md') {
console.log('This is a Markdown file');
} else if (file.extension === 'pdf') {
console.log('This is a PDF file');
} else {
console.log('Unknown file type:', file.extension);
}
}
const file = this.app.vault.getFileByPath('MyNote.md');
if (file) {
// Get file age in days
const ageInDays = (Date.now() - file.stat.ctime) / (1000 * 60 * 60 * 24);
console.log(`File is ${ageInDays.toFixed(1)} days old`);
// Check if file was recently modified (within last hour)
const hourInMs = 60 * 60 * 1000;
const recentlyModified = (Date.now() - file.stat.mtime) < hourInMs;
console.log('Recently modified:', recentlyModified);
}
Filtering files by extension
// Get all image files
const imageExtensions = ['png', 'jpg', 'jpeg', 'gif', 'svg'];
const imageFiles = this.app.vault.getFiles().filter(file =>
imageExtensions.includes(file.extension)
);
console.log(`Found ${imageFiles.length} images`);
Reading file content
const file = this.app.vault.getFileByPath('MyNote.md');
if (file) {
if (file.extension === 'md') {
// Read text file
const content = await this.app.vault.read(file);
console.log(content);
} else {
// Read binary file
const buffer = await this.app.vault.readBinary(file);
console.log('Binary data length:', buffer.byteLength);
}
}
- Vault - Main class for working with files
- TFolder - Represents a folder in the vault
- FileManager - Higher-level file operations