【问题标题】:How to remove all files from directory without removing the directory in Deno?如何从目录中删除所有文件而不删除 Deno 中的目录?
【发布时间】:2021-07-13 04:15:24
【问题描述】:

如何在不删除目录本身的情况下使用 Deno 从目录中删除所有文件?

【问题讨论】:

    标签: javascript deno


    【解决方案1】:

    这可以通过fs 模块轻松完成:

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

    也可以在不使用fs 模块的情况下迭代文件以获得更精细的控制:

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

    在任何情况下都需要--allow-read--allow-write 权限。

    【讨论】:

      猜你喜欢
      • 2015-01-20
      • 2012-03-05
      • 2013-01-07
      • 1970-01-01
      • 1970-01-01
      • 2010-11-05
      • 1970-01-01
      • 1970-01-01
      • 2017-05-13
      相关资源
      最近更新 更多