Skip to content

FileManager

Manage the creation, deletion and renaming of files from the UI.

Constructors

new FileManager()

new FileManager(): FileManager

Returns

FileManager

Properties

app

app: App

Reference to App


vault

vault: Vault

Reference to Vault

Methods

canCreateFileWithExt()

canCreateFileWithExt(arg1): unknown

Parameters

arg1: unknown

Returns

unknown


createAndOpenMarkdownFile()

createAndOpenMarkdownFile(path, location): Promise<void>

Creates a new Markdown file in specified location and opens it in a new view

Parameters

path: string

Path to the file to create (missing folders will be created)

location: PaneType

Where to open the view containing the new file

Returns

Promise<void>


createNewFile()

createNewFile(location, filename, extension, contents): Promise<TFile>

Create a new file in the vault at specified location

Parameters

location: TFolder

Location to create the file in, defaults to root

filename: string

Name of the file to create, defaults to ‘Untitled’ (incremented if file already exists)

extension: string

Extension of the file to create, defaults to ‘md’

contents: string

Contents of the file to create, defaults to empty string

Returns

Promise<TFile>


createNewFolder()

createNewFolder(location): Promise<TFolder>

Creates a new untitled folder in the vault at specified location

Parameters

location: TFolder

Location to create the folder in, defaults to root

Returns

Promise<TFolder>


createNewMarkdownFile()

createNewMarkdownFile(location, filename, contents): Promise<TFile>

Creates a new Markdown file in the vault at specified location

Parameters

location: TFolder

filename: string

contents: string

Returns

Promise<TFile>


createNewMarkdownFileFromLinktext()

createNewMarkdownFileFromLinktext(filename, path): Promise<TFile>

Creates a new Markdown file based on linktext and path

Parameters

filename: string

Name of the file to create

path: string

Path to where the file should be created

Returns

Promise<TFile>


generateMarkdownLink(file, sourcePath, subpath?, alias?): string

Generate a Markdown link based on the user’s preferences.

Parameters

file: TFile

the file to link to.

sourcePath: string

where the link is stored in, used to compute relative links.

subpath?: string

A subpath, starting with #, used for linking to headings or blocks.

alias?: string

The display text if it’s to be different than the file name. Pass empty string to use file name.

Returns

string


getAvailablePathForAttachment()

getAvailablePathForAttachment(filename, sourcePath?): Promise<string>

Resolves a unique path for the attachment file being saved. Ensures that the parent directory exists and dedupes the filename if the destination filename already exists.

Parameters

filename: string

Name of the attachment being saved

sourcePath?: string

The path to the note associated with this attachment, defaults to the workspace’s active file.

Returns

Promise<string>

Full path for where the attachment should be saved, according to the user’s settings


getMarkdownNewFileParent()

getMarkdownNewFileParent(path): TFolder

Gets the folder that new markdown files should be saved to, based on the current settings

Parameters

path: string

The path of the current opened/focused file, used when the user wants new files to be created in the same folder as the current file

Returns

TFolder


getNewFileParent()

getNewFileParent(sourcePath, newFilePath?): TFolder

Gets the folder that new files should be saved to, given the user’s preferences.

Parameters

sourcePath: string

The path to the current open/focused file, used when the user wants new files to be created ‘in the same folder’. Use an empty string if there is no active file.

newFilePath?: string

The path to the file that will be newly created, used to infer what settings to use based on the path’s extension.

Returns

TFolder


insertIntoFile()

insertIntoFile(arg1, arg2, arg3): unknown

Parameters

arg1: unknown

arg2: unknown

arg3: unknown

Returns

unknown


iterateAllRefs()

iterateAllRefs(callback): void

Iterate over all links in the vault with callback

Parameters

callback

Callback to execute for each link

Returns

void


mergeFile()

mergeFile(file, otherFile, override, atStart): Promise<void>

Merge two files

Parameters

file: TFile

File to merge to

otherFile: TFile

File to merge from

override: string

If not empty, will override the contents of the file with this string

atStart: boolean

Whether to insert text at the start or end of the file

Returns

Promise<void>


processFrontMatter()

processFrontMatter(file, fn, options?): Promise<void>

Atomically read, modify, and save the frontmatter of a note. The frontmatter is passed in as a JS object, and should be mutated directly to achieve the desired result.

Remember to handle errors thrown by this method.

Parameters

file: TFile

the file to be modified. Must be a Markdown file.

fn

a callback function which mutates the frontmatter object synchronously.

options?: DataWriteOptions

write options.

Returns

Promise<void>

Throws

YAMLParseError if the YAML parsing fails

Throws

any errors that your callback function throws

Example

app.fileManager.processFrontMatter(file, (frontmatter) => {
frontmatter['key1'] = value;
delete frontmatter['key2'];
});

promptForDeletion()

promptForDeletion(file): Promise<void>

Prompt the user to delete a file

Parameters

file: TFile

Returns

Promise<void>


promptForFileDeletion()

promptForFileDeletion(arg1): Promise<unknown>

Parameters

arg1: unknown

Returns

Promise<unknown>


promptForFileRename()

promptForFileRename(file): Promise<void>

Prompt the user to rename a file

Parameters

file: TFile

Returns

Promise<void>


promptForFolderDeletion()

promptForFolderDeletion(arg1): Promise<unknown>

Parameters

arg1: unknown

Returns

Promise<unknown>


renameFile()

renameFile(file, newPath): Promise<void>

Rename or move a file safely, and update all links to it depending on the user’s preferences.

Parameters

file: TAbstractFile

the file to rename

newPath: string

the new path for the file

Returns

Promise<void>


renameProperty()

renameProperty(oldKey, newKey): Promise<void>

Rename’s a property for all notes currently that have the old key

Parameters

oldKey: string

newKey: string

Returns

Promise<void>


trashFile()

trashFile(file): Promise<void>

Remove a file or a folder from the vault according the user’s preferred ‘trash’ options (either moving the file to .trash/ or the OS trash bin).

Parameters

file: TAbstractFile

Returns

Promise<void>