Meta
up:: ๐งช Code Diary
type:: #log/code
tags:: #on/snippets
language:: Quickadd
program:: Obsidian
topics:: Coding
Move a specific file to a folder with a command or click
Language:: Quickadd
Program:: Obsidian
URL:: Script to move current file to a specific folder ยท chhoumann/quickadd ยท Discussion #386 ยท GitHub
Summary
How to move a file to a specific folder with a single command / click.
Code
Recently, a question popped up on the Obsidian discord about how to move a file to a specific folder with a single command / click.
Here's a script you can use to do that. Just add it to a macro and bind that to a command/hotkey.
const targetDir = "targetFolderHere"; // e.g. '1 - Areas'. Don't end with a slash. Empty for root directory.
module.exports = async () => {
const currentFile = app.workspace.getActiveFile();
if (!currentFile) {
new Notice(`No active file to move.`);
return;
}
if (currentFile.parent.name === targetDir) {
new Notice(`File is already in target folder.`);
return;
}
try {
await app.vault.rename(currentFile, `${targetDir}/${currentFile.name}`);
} catch (error) {
new Notice(`Error moving file: ${error}`, 15000);
const folder = app.vault.getAbstractFileByPath(targetDir);
if (!folder) {
new Notice(`Target folder '${targetDir}' does not exist.`, 15000);
}
}
};
up:: ๐งช Code Diary