Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/ayonli_jsext/fs.ts>copy

A JavaScript extension package for building strong and modern applications.
Latest
function copy
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

// with the default storage
import { copy } from "@ayonli/jsext/fs";

await copy("/path/to/old.txt", "/path/to/new.txt");

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

// 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

// 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"));

Parameters

src: string
dest: string
optional
options: CopyOptions

Returns

Promise<void>

Examples

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);

Parameters

src: FileSystemFileHandle
dest: FileSystemFileHandle | FileSystemDirectoryHandle

Returns

Promise<void>

Examples

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 });

Parameters

src: FileSystemDirectoryHandle
dest: FileSystemDirectoryHandle
optional
options: Pick<CopyOptions, "recursive">

Returns

Promise<void>