Copy file between vaults with/including attachments

My “Quick and Dirty Way” to copy a file and/with/including all it’s attachments/images to another vault.

  1. use the dataview plugin
  2. paste the dataview snipped below into the file you want to copy over to another vault
  3. copy the dataview output to the clipboard
  4. remove the pasted dataview code of step 2 from the file
  5. in a terminal shell cd your vault’s base folder
  6. create the subfolders needed below <basePathOfOtherVault>
  7. cp <pasteHere> <basePathOfOtherVault>
LIST WITHOUT ID join(list("\"",
                    join(list(
                        string(file.path),
                        join(map(file.outlinks, (x) => meta(x).path), "\" \"")
                    ), "\" \""),
                "\""), "")
WHERE file.path = this.file.path

rendered dataview result is a list with one(!) element:
first the quoted file path/name.md of the file you want to copy,
and then quoted file path/names of all attachments, e.g.:

  • “FileToMove.md” “_binary/FirstAttachment.png” “_resources/someFolder/SecondAttachment.png”

if the second element just states "" that means there are no attachments.

Works fine in my case as I have all attachments inside the same _binaries folder and NOT in folders below the file.

let me know what you think on this (dirty but works!)

1 Like

okay, fine tuned a similar but more complete solution myself.

Following dataview generates bash shell like unix cp commands including creation of the file folders inside the targetdir with mkdir -p and dirname:

in you shell you have to set the target dir as unix environment variable, e.g.:

export targetdir='path/to/your/vault'
LIST WITHOUT ID
flat(list(
    list(
        join(list("mkdir -p \"", "\$targetdir/$(dirname '", string(file.path) , "')\""), ""),
        join(list("cp '", string(file.path), "' \"", "$targetdir/", string(file.path), "\""), "")
    ),
    map(file.outlinks, (x) =>
        join(list("mkdir -p \"", "\$targetdir/$(dirname '", meta(x).path , "')\""), "")
    ),
    map(file.outlinks, (x) =>
        join(list("cp '", meta(x).path, "' \"$targetdir/", meta(x).path, "\""), "")
    )
))
WHERE file.path = this.file.path

result may look something like this: (for one file with two attached images)

    • mkdir -p “$targetdir/$(dirname ‘basefile (that you want to copy).md’)”
    • cp ‘basefile (that you want to copy).md’ “$targetdir/basefile (that you want to copy).md”
    • mkdir -p “$targetdir/$(dirname ‘_resources/C64_Computer_Image_Photo_Foto.resources/C64.png’)”
    • mkdir -p “$targetdir/$(dirname ‘_binary/ObsidianLogo.svg’)”
    • cp ‘_resources/C64_Computer_Image_Photo_Foto.resources/C64.png’ “$targetdir/_resources/C64_Computer_Image_Photo_Foto.resources/C64.png”
    • cp ‘_binary/ObsidianLogo.svg’ “$targetdir/_binary/ObsidianLogo.svg”

copy and paste to your terminal shell will just copy the commands one per line (at least in my iterm)

1 Like

… might also be of use.

AFAIK that plugin only moves within the same vault.
It is not able to move incl. attachments to some other vault, or does it??

1 Like

Yes, you are right.
One would keep one’s vault consistent by using the plugin and then just use the underlying file system of the OS to copy & paste it over.
I personally haven’t had to use the plugin because I keep my vault such that I can easily open it in e.g. Markor Android app or Windows/Linux VS Code.
A couple of users have found it useful to be able to view their “lost” attachments.

Perhaps that is the strength of your method … one does not have to have ““Default location for new attachments”:In subfolder under current folder.” to copy files over to another vault.

1 Like

just out of curiosity: what are the advantages of Markor on Android vs the obsidian app on Android, so that you’ve chosen to use Markor over obsidian Android app?

I primarily use the Obsidian Android app.

I use the Markor Android app and its QuickNote for quick entry without having to open Obsidian or when due to low RAM it has dropped out of memory & relaunching the app+plugins+workspaces is not needed & would take too long. I also like using Markor & VS Code to check that my notes are usable outside of Obsidian.

My Markor settings make it access my Obsidian vault files location:

Obsidian.PNG | Markor settings.PNG

I use the following settings for all my Obsidian vaults …

Under Files and Links:

  1. “Default location for new notes” - “Same folder as current file”.
  2. “New Link Format” - “Relative path to file”.
  3. “Use [[Wikilinks]]” - “off” … yet I still get the advantages of [[]]
  4. Detect all file extensions - enabled
  5. “Default location for new attachments” - “In subfolder under current folder”.
  6. “Subfolder name” - value: “attachments”.
1 Like

Responding here, because these other related threads are closed

Moving notes, with attachments, to new vault - Help - Obsidian Forum
Moving notes from one vault to another - Help - Obsidian Forum

If support for moving files between vaults is added, I think the attachments shouldn’t always be copied. IMO attachments should be copied to the new vault(s) when there are still files in the original vault that reference the attachment, and moved to the new vaults(s) and deleted from the original vault when there are no references to it.

1 Like