The DataAdapter interface provides low-level access to the file system. It is implemented by platform-specific adapters like FileSystemAdapter (desktop) and CapacitorAdapter (mobile). If possible, prefer using the Vault API over this interface.
Implementations
- FileSystemAdapter - Desktop implementation
- CapacitorAdapter - Mobile implementation (iOS/Android)
Methods
getName
Get the name of the adapter.
Returns: The adapter name (e.g., “file-system”, “capacitor”).
exists
exists(normalizedPath: string, sensitive?: boolean): Promise<boolean>
Check if something exists at the given path. For a faster way to synchronously check if a note or attachment is in the vault, use Vault.getAbstractFileByPath.
Path to file/folder, use normalizePath() to normalize beforehand.
Some file systems/operating systems are case-insensitive, set to true to force a case-sensitivity check.
Returns: Promise resolving to true if the path exists.
stat
stat(normalizedPath: string): Promise<Stat | null>
Retrieve metadata about the given file/folder.
Path to file/folder, use normalizePath() to normalize beforehand.
Returns: Promise resolving to file/folder stats, or null if not found.
Since: 0.12.2
interface Stat {
type: 'file' | 'folder';
ctime: number; // Creation time (unix timestamp)
mtime: number; // Modification time (unix timestamp)
size: number; // Size in bytes
}
list
list(normalizedPath: string): Promise<ListedFiles>
Retrieve a list of all files and folders inside the given folder, non-recursive.
Path to folder, use normalizePath() to normalize beforehand.
Returns: Promise resolving to lists of files and folders.
interface ListedFiles {
files: string[]; // Array of file paths
folders: string[]; // Array of folder paths
}
read
read(normalizedPath: string): Promise<string>
Read the contents of a plaintext file.
Path to file, use normalizePath() to normalize beforehand.
Returns: Promise resolving to the file contents.
readBinary
readBinary(normalizedPath: string): Promise<ArrayBuffer>
Read the contents of a binary file.
Path to file, use normalizePath() to normalize beforehand.
Returns: Promise resolving to the binary file contents.
write
write(normalizedPath: string, data: string, options?: DataWriteOptions): Promise<void>
Write to a plaintext file. If the file exists its content will be overwritten, otherwise the file will be created.
Path to file, use normalizePath() to normalize beforehand.
Optional write options (ctime, mtime).
Returns: Promise that resolves when the operation is complete.
writeBinary
writeBinary(normalizedPath: string, data: ArrayBuffer, options?: DataWriteOptions): Promise<void>
Write to a binary file. If the file exists its content will be overwritten, otherwise the file will be created.
Path to file, use normalizePath() to normalize beforehand.
Optional write options (ctime, mtime).
Returns: Promise that resolves when the operation is complete.
append
append(normalizedPath: string, data: string, options?: DataWriteOptions): Promise<void>
Add text to the end of a plaintext file.
Path to file, use normalizePath() to normalize beforehand.
Returns: Promise that resolves when the operation is complete.
appendBinary
appendBinary(normalizedPath: string, data: ArrayBuffer, options?: DataWriteOptions): Promise<void>
Add data to the end of a binary file.
Path to file, use normalizePath() to normalize beforehand.
Returns: Promise that resolves when the operation is complete.
Since: 1.12.3
process
process(normalizedPath: string, fn: (data: string) => string, options?: DataWriteOptions): Promise<string>
Atomically read, modify, and save the contents of a plaintext file.
Path to file, use normalizePath() to normalize beforehand.
fn
(data: string) => string
required
A callback function which returns the new content of the file synchronously.
Returns: Promise resolving to the text value of the file that was written.
getResourcePath
getResourcePath(normalizedPath: string): string
Returns a URI for the browser engine to use, for example to embed an image.
Path to file, use normalizePath() to normalize beforehand.
Returns: A URI string.
mkdir
mkdir(normalizedPath: string): Promise<void>
Create a directory.
Path to use for new folder, use normalizePath() to normalize beforehand.
Returns: Promise that resolves when the operation is complete.
trashSystem
trashSystem(normalizedPath: string): Promise<boolean>
Try moving to system trash.
Path to file/folder, use normalizePath() to normalize beforehand.
Returns: Promise resolving to true if succeeded. This can fail due to system trash being disabled.
trashLocal
trashLocal(normalizedPath: string): Promise<void>
Move to local trash. Files will be moved into the .trash folder at the root of the vault.
Path to file/folder, use normalizePath() to normalize beforehand.
Returns: Promise that resolves when the operation is complete.
rmdir
rmdir(normalizedPath: string, recursive: boolean): Promise<void>
Remove a directory.
Path to folder, use normalizePath() to normalize beforehand.
If true, delete folders under this folder recursively. If false, the folder needs to be empty.
Returns: Promise that resolves when the operation is complete.
remove
remove(normalizedPath: string): Promise<void>
Delete a file.
Path to file, use normalizePath() to normalize beforehand.
Returns: Promise that resolves when the operation is complete.
rename
rename(normalizedPath: string, normalizedNewPath: string): Promise<void>
Rename a file or folder.
Current path to file/folder, use normalizePath() to normalize beforehand.
New path to file/folder, use normalizePath() to normalize beforehand.
Returns: Promise that resolves when the operation is complete.
copy
copy(normalizedPath: string, normalizedNewPath: string): Promise<void>
Create a copy of a file. This will fail if there is already a file at normalizedNewPath.
Path to file, use normalizePath() to normalize beforehand.
Path for the copy, use normalizePath() to normalize beforehand.
Returns: Promise that resolves when the operation is complete.
Types
DataWriteOptions
interface DataWriteOptions {
ctime?: number; // Creation time (unix timestamp in milliseconds)
mtime?: number; // Modification time (unix timestamp in milliseconds)
}
Optional options for write operations to control file timestamps.
Usage
Access the adapter through the Vault:
const adapter = this.app.vault.adapter;
console.log('Adapter name:', adapter.getName());
Examples
Using the adapter directly
import { normalizePath } from 'obsidian';
const adapter = this.app.vault.adapter;
const path = normalizePath('MyFolder/MyFile.md');
// Check if file exists
const exists = await adapter.exists(path);
console.log('File exists:', exists);
if (exists) {
// Read file
const content = await adapter.read(path);
console.log('Content:', content);
// Get file stats
const stat = await adapter.stat(path);
console.log('File size:', stat?.size);
}
Listing directory contents
import { normalizePath } from 'obsidian';
const adapter = this.app.vault.adapter;
const folderPath = normalizePath('MyFolder');
const listed = await adapter.list(folderPath);
console.log('Files:', listed.files);
console.log('Folders:', listed.folders);
const adapter = this.app.vault.adapter;
if (adapter instanceof FileSystemAdapter) {
// Desktop-specific: Get base path
const basePath = adapter.getBasePath();
console.log('Vault location:', basePath);
}
if (adapter instanceof CapacitorAdapter) {
// Mobile-specific operations
console.log('Running on mobile');
}
Writing with custom timestamps
import { normalizePath } from 'obsidian';
const adapter = this.app.vault.adapter;
const path = normalizePath('MyFile.md');
const customTime = new Date('2024-01-01').getTime();
await adapter.write(path, 'File content', {
ctime: customTime,
mtime: customTime
});
console.log('File created with custom timestamp');
Notes
- Always use
normalizePath() to normalize paths before passing them to adapter methods.
- The adapter works with vault-relative paths, not absolute file system paths.
- Prefer using the Vault API when possible, as it provides additional features like event notifications and better integration with Obsidian’s caching system.
- Vault - Higher-level file operations
- TFile - File representation
- TFolder - Folder representation