Skip to content

file-system

This module provides utility functions for working with TAbstractFile, TFile, and TFolder instances in Obsidian.

Interface Description
DoesExistParams Parameters for doesExist.
GetAbstractFileOrNullParams Parameters for getAbstractFileOrNull.
GetAbstractFileParams Parameters for getAbstractFile.
GetBasenameParams Parameters for getBasename.
GetExtensionParams Parameters for getExtension.
GetFileOrNullParams Parameters for getFileOrNull.
GetFileParams Parameters for getFile.
GetFolderOrNullParams Parameters for getFolderOrNull.
GetFolderParams Parameters for getFolder.
GetMarkdownFilesParams Parameters for getMarkdownFiles.
IsTreatedAsAttachmentParams Parameters for isTreatedAsAttachment.
Function Description
asArrayOfFiles Converts an array of abstract files to an array of files.
asArrayOfFolders Converts an array of abstract files to an array of folders.
asFile Converts an abstract file to a file.
asFileOrNull Converts an abstract file to a file or null.
asFolder Converts an abstract file to a folder.
asFolderOrNull Converts an abstract file to a folder or null.
checkExtension Checks if the given path or file has the specified extension.
doesExist Checks if the given path exists.
getAbstractFile Retrieves the TAbstractFile object for the given path or abstract file.
getAbstractFileOrNull Retrieves an abstract file or null based on the provided path or abstract file.
getBasename Returns the base name of the given pathOrFile: a file’s name without its extension, a folder’s full name. Unlike basename from path.ts, which is a pure string function, this resolves the argument against the vault first. That is what lets a folder whose name contains a dot answer with its whole name instead of a bogus extension split, and it is why a caller handling both kinds no longer has to branch on isFile itself — a folder has no basename of its own. A path that resolves to nothing has no answer to look up, so GetBasenameParams.whenMissingTreatAs decides what happens. It defaults to MissingPathTreatment.Throw rather than to a guess: alpha.bravo may name a dotted folder or a .bravo file, and nothing in the vault says which, so a silently wrong answer is worse than an error. Pass MissingPathTreatment.File for the checkExtension reading of a bare string, or MissingPathTreatment.Folder for the other one.
getExtension Returns the extension of the given pathOrFile: a file’s extension without the leading dot, and an empty string for a folder. Unlike extname from path.ts, which is a pure string function, this resolves the argument against the vault first, so a folder whose name contains a dot answers with an empty string rather than a bogus extension. A folder has no extension of its own, which is why a caller handling both kinds would otherwise branch on isFile itself. A path that resolves to nothing has no answer to look up, so GetExtensionParams.whenMissingTreatAs decides what happens. It defaults to MissingPathTreatment.Throw rather than to a guess: alpha.bravo may name a dotted folder or a .bravo file, and nothing in the vault says which, so a silently wrong answer is worse than an error. Pass MissingPathTreatment.File for the checkExtension reading of a bare string, or MissingPathTreatment.Folder for the other one.
getFile Retrieves a file based on the provided path or file.
getFileOrNull Retrieves a file or null based on the provided path or file. If the provided argument is already a file, it is returned as is. Otherwise, the function uses the app’s vault to retrieve the file by its path.
getFileSystemType Gets the type of a file system object.
getFolder Retrieves a folder based on the provided app and pathOrFolder.
getFolderOrNull Retrieves a folder or null based on the provided path or folder.
getMarkdownFiles Retrieves an array of files representing the markdown files within a specified folder or path.
getOrCreateFile Retrieves the file for the given path or creates a new one if it does not exist.
getOrCreateFolder Retrieves the folder for the given path or creates a new one if it does not exist.
getPath Returns the path of the given pathOrFile.
isAbstractFile Checks if the given file is an instance of abstract file.
isBaseFile Checks if the given file is a base file.
isCanvasFile Checks if the given file is a canvas file.
isFile Checks if the given file is an instance of file.
isFolder Checks if the given file is a folder.
isMarkdownFile Checks if the given file is a Markdown file.
isNote Checks if the given file is a note.
isPathOrFile Checks if the given value is a path or a file.
isTreatedAsAttachment Checks if the given file should be treated as an attachment because of its extension. Some plugins store their data in files that are markdown on disk but are not prose — Excalidraw, for example, saves each drawing as sketch.excalidraw.md. Obsidian reports such a file’s extension as md, so a caller that wants to tell a drawing apart from a note has to match the full multi-part extension (.excalidraw.md) against the file name. Which extensions count is the caller’s configuration, not this library’s — only the matching rule is shared. A drawing can also be recognized by its frontmatter, the way Excalidraw itself recognizes one (excalidraw-plugin: parsed), through a property:name[=value] entry. That answer comes from the metadata cache, which can be cold at startup or right after the file is created, so an uncached file answers “no match” and is treated as a note. That is the conservative side for every current caller: a note is not moved as an attachment, and it spares its folder from a delete sweep.
trimMarkdownExtension Trims the markdown extension from the file path if the file is a markdown file. If the file is not a markdown file, the original file path is returned.
Type Description
PathOrAbstractFile A path or an abstract file.
PathOrFile A path or a file.
PathOrFolder A path or a folder.
Enum Description
FileSystemType A type of file system object.
MissingPathTreatment How getBasename and getExtension should answer for a path that resolves to nothing in the vault. Nothing in the vault says whether an unresolved alpha.bravo names a dotted folder or a .bravo file, so the caller decides.
Variable Description
BASE_FILE_EXTENSION A file extension for base files.
CANVAS_FILE_EXTENSION A file extension for canvas files.
MARKDOWN_FILE_EXTENSION A file extension for markdown files.