node.js delete directory & file system

delete a not empty directory

https://nodejs.org/api/fs.htm

fs.rmdir(path[, options], callback)
fs.rmdirSync(path[, options])

recursive: true

In a Node.js application, you can use the fs.rmdir() method to delete a directory.

This method works asynchronously to remove the directory.

If the directory is not empty, you can pass an optional recursive flag to delete all nested files and folders recursively.

const fs = require('fs');

// directory path
const dir = 'temp'; // delete directory recursively
fs.rmdir(dir, { recursive: true }, (err) => {
if (err) {
throw err;
} console.log(`${dir} is deleted!`);
});
const fs = require('fs');

// directory path
const dir = 'temp'; // delete directory recursively
try {
fs.rmdirSync(dir, { recursive: true }); console.log(`${dir} is deleted!`);
} catch (err) {
console.error(`Error while deleting ${dir}.`);
}

del

https://www.npmjs.com/package/del

$ npm i -D del

https://github.com/sindresorhus/del/blob/master/index.js

trash

https://github.com/sindresorhus/trash/blob/master/index.js

rimraf

https://www.npmjs.com/package/rimraf

$ npm i -D rimraf

https://github.com/isaacs/rimraf/blob/master/rimraf.js

refs

https://attacomsian.com/blog/nodejs-delete-directory

https://geedew.com/remove-a-directory-that-is-not-empty-in-nodejs/


var fs = require('fs');
var deleteFolderRecursive = function(path) {
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};

unlink

https://stackoverflow.com/questions/39963966/can-fs-unlink-delete-a-empty-or-non-empty-folder

https://stackoverflow.com/questions/18052762/remove-directory-which-is-not-empty/32197381

refs



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


node.js delete directory & file system的更多相关文章

  1. [Node.js] Read a File in Node.js with fs.readFile and fs.readFileSync

    We'll read a csv file in node.js both synchronously, and asynchronously. The file we're reading is a ...

  2. Getting Started with Mongoose and Node.js – A Sample Comments System | Dev Notes

    In this post, we’re going to be creating a sample comments system using Node, Express and Mongoose.  ...

  3. [Node.js] Trigger a File Download in Express

    Downloading and saving a file is a common scenario when building out your web application. Using Exp ...

  4. Node.js NPM Tutorial

    Node.js NPM Tutorial – How to Get Started with NPM? NPM is the core of any application that is devel ...

  5. Node.js Web 开发框架大全《静态文件服务器篇》

    这篇文章与大家分享优秀的 Node.js 静态服务器模块.Node 是一个服务器端 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用程序,编写能 ...

  6. node.js监听文件变化

    前言 随着前端技术的飞速发展,前端开发也从原始的刀耕火种,向着工程化效率化的方向发展.在各种开发框架之外,打包编译等技术也是层出不穷,开发体验也是越来越好.例如HMR,让我们的更新可以即时可见,告别了 ...

  7. 极简 Node.js 入门 - 3.1 File System API 风格

    极简 Node.js 入门系列教程:https://www.yuque.com/sunluyong/node 本文更佳阅读体验:https://www.yuque.com/sunluyong/node ...

  8. 解决Warning: unlink(/storage/cache/cache.catalog.language.1556158719): No such file or directory in /system/library/cache/file.php on line 68问题

    ytkah在调试opencart项目时提示Warning: unlink(/storage/cache/cache.catalog.language.1556158719): No such file ...

  9. node.js & create file

    node.js & create file node js create file if not exists https://nodejs.org/api/fs.html#fs_fs_ope ...

随机推荐

  1. gradles理解和文件配置

    gradle gradle tasks :查看所有的taske命令 bootJar:打包,讲项目的所有依赖和主工程代码打包,一个可直接执行的一个包,不需要tomcat运行 gradle使用bootja ...

  2. hadoop及NameNode和SecondaryNameNode工作机制

    hadoop及NameNode和SecondaryNameNode工作机制 1.hadoop组成 Common MapReduce Yarn HDFS (1)HDFS namenode:存放目录,最重 ...

  3. 思考gRPC :为什么是HTTP/2

    Introducing gRPC Support with NGINX 1.13.10 - NGINX https://www.nginx.com/blog/nginx-1-13-10-grpc/ 思 ...

  4. 线上nginx的一次“no live upstreams while connecting to upstream ”分析

    线上nginx的一次"no live upstreams while connecting to upstream "分析 线上nginx的一次"no live upst ...

  5. Python基础(函数)

    为什么要用函数? 解决代码重用问题 统一维护 程序的组织结构清晰,可读性强函数:先定义后使用1.内置函数 built-in function eg:sum max len2.自定义函数:定义有参函数 ...

  6. 题解 P3833 【[SHOI2012]魔法树】

    题目 直通车 很显然这是个树刨的板子,树上链查询和子树查询 注意: 1.这个点的树根为 0 而不是 1 所以注意读图时点标号 +1 就解决了 2.注意数据范围\(2^{32}\) 然后板子就能过了 n ...

  7. COGS 307 模拟退火

    307. [HAOI2006] 均分数据 ★★   输入文件:data.in   输出文件:data.out   简单对比时间限制:3 s   内存限制:128 MB [问题描述] 已知N个正整数:A ...

  8. zabbix添加持续告警

  9. NetCore控制台程序-使用HostService和HttpClient实现简单的定时爬虫

    .NetCore承载系统 .NetCore的承载系统, 可以将长时间运行的服务承载于托管进程中, AspNetCore应用其实就是一个长时间运行的服务, 启动AspNetCore应用后, 它就会监听网 ...

  10. Codeforces Round #682 (Div. 2)【ABCD】

    比赛链接:https://codeforces.com/contest/1438 A. Specific Tastes of Andre 题意 构造一个任意连续子数组元素之和为子数组长度倍数的数组. ...