简体   繁体   中英

How to remove all files from directory without removing the directory in Deno?

如何使用 Deno 从目录中删除所有文件而不删除目录本身?

This can be done very easily with the fs module:

import * as fs from "https://deno.land/std/fs/mod.ts";
await fs.emptyDir("path/to/dir");

It's also possible to iterate the files for some finer control without using the fs module:

import * as path from "https://deno.land/std/path/mod.ts";

const dirPath = path.join("path", "to", "dir"); // “path/to/dir”
for await(const dirEntry of Deno.readDir(dirPath)) {
    await Deno.remove(path.join(dirPath, dirEntry.name), { recursive: true });
}

Requires --allow-read and --allow-write permissions, in any case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM