import { copy } from "https://deno.land/x/ayonli_jsext@v0.9.72/fs.ts";
Copies the file or directory (and its contents) from the old location to the new location.
NOTE: If the old location is a file and the new location is a directory, the file will be copied into the new directory with the old name.
NOTE: In Unix/Linux systems, when using the cp -R
command to copy a path
without an ending slash, the command will copy the directory itself into the
new path if the new path already exists. This function does not have this
behavior, it does not distinguish between a path with a trailing slash and a
path without it. So when copying a directory, this function always copy its
contents to the new path, whether the new path already exists or not.
Examples
Example 1
Example 1
// with the default storage
import { copy } from "@ayonli/jsext/fs";
await copy("/path/to/old.txt", "/path/to/new.txt");
Example 2
Example 2
// with a user-selected directory as root (Chromium only)
import { copy } from "@ayonli/jsext/fs";
const root = await window.showDirectoryPicker();
await copy("/path/to/old.txt", "/path/to/new.txt", { root });
Example 3
Example 3
// copy a directory and its contents recursively
import { copy } from "@ayonli/jsext/fs";
await copy("/path/to/dir", "/path/to/new", { recursive: true });
Example 4
Example 4
// copy a file to a directory
import { copy, exists } from "@ayonli/jsext/fs";
await copy("/path/to/file.txt", "/path/to/dir");
console.assert(await exists("/path/to/dir/file.txt"));
Examples
Example 1
Example 1
// copy a file from the device's file system to the browser's file system (Chromium only)
import { copy, getFileHandle } from "@ayonli/jsext/fs";
const file1 = await window.showOpenFilePicker();
const file2 = await getFileHandle("/path/to/file.txt");
await copy(file1[0], file2);
Examples
Example 1
Example 1
// copy a directory from the device's file system to the browser's file system (Chromium only)
import { copy, getDirHandle } from "@ayonli/jsext/fs";
const dir1 = await window.showDirectoryPicker();
const dir2 = await getDirHandle("/path/to/dir");
await copy(dir1, dir2, { recursive: true });