The Vault class is the primary interface for working with files and folders in an Obsidian vault. It provides methods for creating, reading, modifying, and deleting files, as well as listening to file system events.
Properties
The data adapter that handles low-level file system operations.
Path to the config folder. This value is typically .obsidian but it could be different.
Methods
getName
Gets the name of the vault.
Returns: The vault name.
Since: 0.9.7
getFileByPath
getFileByPath(path: string): TFile | null
Get a file inside the vault at the given path. Returns null if the file does not exist.
Vault absolute path to the file.
Returns: The file if found, or null.
Since: 1.5.7
getFolderByPath
getFolderByPath(path: string): TFolder | null
Get a folder inside the vault at the given path. Returns null if the folder does not exist.
Vault absolute path to the folder.
Returns: The folder if found, or null.
Since: 1.5.7
getAbstractFileByPath
getAbstractFileByPath(path: string): TAbstractFile | null
Get a file or folder inside the vault at the given path. To check if the return type is a file, use instanceof TFile. To check if it is a folder, use instanceof TFolder.
Vault absolute path to the folder or file, with extension, case sensitive.
Returns: The abstract file if found, or null.
Since: 0.11.11
getRoot
Get the root folder of the current vault.
Returns: The root folder.
Since: 0.9.7
create
create(path: string, data: string, options?: DataWriteOptions): Promise<TFile>
Create a new plaintext file inside the vault.
Vault absolute path for the new file, with extension.
Text content for the new file.
Returns: Promise resolving to the created file.
Throws: Error if file already exists.
Since: 0.9.7
createBinary
createBinary(path: string, data: ArrayBuffer, options?: DataWriteOptions): Promise<TFile>
Create a new binary file inside the vault.
Vault absolute path for the new file, with extension.
Binary content for the new file.
Returns: Promise resolving to the created file.
Throws: Error if file already exists.
Since: 0.9.7
createFolder
createFolder(path: string): Promise<TFolder>
Create a new folder inside the vault.
Vault absolute path for the new folder.
Returns: Promise resolving to the created folder.
Throws: Error if folder already exists.
Since: 1.4.0
read
read(file: TFile): Promise<string>
Read a plaintext file that is stored inside the vault, directly from disk. Use this if you intend to modify the file content afterwards. Use cachedRead() otherwise for better performance.
Returns: Promise resolving to the file contents.
Since: 0.9.7
cachedRead
cachedRead(file: TFile): Promise<string>
Read the content of a plaintext file stored inside the vault. Use this if you only want to display the content to the user. If you want to modify the file content afterward use read().
Returns: Promise resolving to the file contents.
Since: 0.9.7
readBinary
readBinary(file: TFile): Promise<ArrayBuffer>
Read the content of a binary file stored inside the vault.
Returns: Promise resolving to the binary file contents.
Since: 0.9.7
getResourcePath
getResourcePath(file: TFile): string
Returns a URI for the browser engine to use, for example to embed an image.
The file to get the resource path for.
Returns: A URI string.
Since: 0.9.7
delete
delete(file: TAbstractFile, force?: boolean): Promise<void>
Deletes the file or folder completely.
The file or folder to be deleted.
Should attempt to delete folder even if it has hidden children.
Returns: Promise that resolves when deletion is complete.
Since: 0.9.7
trash
trash(file: TAbstractFile, system: boolean): Promise<void>
Tries to move to system trash. If that isn’t successful/allowed, use local trash.
The file or folder to be deleted.
Set to false to use local trash by default.
Returns: Promise that resolves when the operation is complete.
Since: 0.9.7
rename
rename(file: TAbstractFile, newPath: string): Promise<void>
Rename or move a file. To ensure links are automatically renamed, use FileManager.renameFile() instead.
Vault absolute path to move file to.
Returns: Promise that resolves when the operation is complete.
Since: 0.9.11
modify
modify(file: TFile, data: string, options?: DataWriteOptions): Promise<void>
Modify the contents of a plaintext file.
Returns: Promise that resolves when the operation is complete.
Since: 0.9.7
modifyBinary
modifyBinary(file: TFile, data: ArrayBuffer, options?: DataWriteOptions): Promise<void>
Modify the contents of a binary file.
Returns: Promise that resolves when the operation is complete.
Since: 0.9.7
append
append(file: TFile, data: string, options?: DataWriteOptions): Promise<void>
Add text to the end of a plaintext file inside the vault.
Returns: Promise that resolves when the operation is complete.
Since: 0.13.0
appendBinary
appendBinary(file: TFile, data: ArrayBuffer, options?: DataWriteOptions): Promise<void>
Add data to the end of a binary file inside the vault.
Returns: Promise that resolves when the operation is complete.
Since: 1.12.3
process
process(file: TFile, fn: (data: string) => string, options?: DataWriteOptions): Promise<string>
Atomically read, modify, and save the contents of a note.
The file to be read and modified.
fn
(data: string) => string
required
A callback function which returns the new content of the note synchronously.
Returns: Promise resolving to the text value of the note that was written.
Since: 1.1.0
Example:
await app.vault.process(file, (data) => {
return data.replace('Hello', 'World');
});
copy
copy<T extends TAbstractFile>(file: T, newPath: string): Promise<T>
Create a copy of a file or folder.
The file or folder to copy.
Vault absolute path for the new copy.
Returns: Promise resolving to the copied file or folder.
Since: 1.8.7
getAllLoadedFiles
getAllLoadedFiles(): TAbstractFile[]
Get all files and folders in the vault.
Returns: Array of all abstract files.
Since: 0.9.7
getAllFolders
getAllFolders(includeRoot?: boolean): TFolder[]
Get all folders in the vault.
Should the root folder (/) be returned.
Returns: Array of all folders.
Since: 1.6.6
getMarkdownFiles
getMarkdownFiles(): TFile[]
Get all Markdown files in the vault.
Returns: Array of all Markdown files.
Since: 0.9.7
getFiles
Get all files in the vault.
Returns: Array of all files.
Since: 0.9.7
recurseChildren
static recurseChildren(root: TFolder, cb: (file: TAbstractFile) => any): void
Recursively traverse a folder and its children.
The folder to start from.
cb
(file: TAbstractFile) => any
required
Callback function called for each file/folder.
Since: 0.9.7
Events
The Vault class extends Events and emits the following events:
create
on(name: 'create', callback: (file: TAbstractFile) => any, ctx?: any): EventRef
Called when a file is created. This is also called when the vault is first loaded for each existing file. If you do not wish to receive create events on vault load, register your event handler inside Workspace.onLayoutReady.
Since: 0.9.7
modify
on(name: 'modify', callback: (file: TAbstractFile) => any, ctx?: any): EventRef
Called when a file is modified.
Since: 0.9.7
delete
on(name: 'delete', callback: (file: TAbstractFile) => any, ctx?: any): EventRef
Called when a file is deleted.
Since: 0.9.7
rename
on(name: 'rename', callback: (file: TAbstractFile, oldPath: string) => any, ctx?: any): EventRef
Called when a file is renamed.
Since: 0.9.7
Examples
Creating and reading a file
// Create a new file
const file = await this.app.vault.create('MyNote.md', '# Hello World');
// Read the file content
const content = await this.app.vault.read(file);
console.log(content); // "# Hello World"
Modifying a file
const file = this.app.vault.getFileByPath('MyNote.md');
if (file) {
await this.app.vault.modify(file, '# Updated Content');
}
Listening to file changes
this.registerEvent(
this.app.vault.on('modify', (file) => {
console.log(`File modified: ${file.path}`);
})
);
Processing a file atomically
const file = this.app.vault.getFileByPath('MyNote.md');
if (file) {
await this.app.vault.process(file, (content) => {
// Replace all instances of 'old' with 'new'
return content.replace(/old/g, 'new');
});
}
Getting all markdown files
const markdownFiles = this.app.vault.getMarkdownFiles();
console.log(`Found ${markdownFiles.length} markdown files`);