Move a linked file into the current folder

Move a linked file into the current folder

Published with Share Note 🔸 Part of Obsidian guides
  1. Add the below template and assign it to a hotkey.
  2. Put your cursor into a link as per the screenshot.
  3. Press the hotkey. It will move the linked file into the same folder as the currently open note.

<%*
const editor = app.workspace.activeEditor
// Get the link at the current cursor position
const link = editor.editor.getClickableTokenAt(editor.editor.getCursor())
if (link?.type === 'internal-link') {
  // Get the current folder
  const currentFolder = editor.file.parent.path
  // Get the linked TFile
  const linkedFile = app.metadataCache.getFirstLinkpathDest(link.text, '')
  // Move the linked file to the current folder
  await app.fileManager.renameFile(linkedFile, currentFolder + '/' + linkedFile.name)
  new Notice(linkedFile.basename + ' has been moved to ' + currentFolder)
}
-%>

Asked in this thread

Share Note for Obsidian 🌓