node.js delete directory & file system
node.js delete directory & file system
delete a not empty directory
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的更多相关文章
- [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 ...
- 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. ...
- [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 ...
- Node.js NPM Tutorial
Node.js NPM Tutorial – How to Get Started with NPM? NPM is the core of any application that is devel ...
- Node.js Web 开发框架大全《静态文件服务器篇》
这篇文章与大家分享优秀的 Node.js 静态服务器模块.Node 是一个服务器端 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用程序,编写能 ...
- node.js监听文件变化
前言 随着前端技术的飞速发展,前端开发也从原始的刀耕火种,向着工程化效率化的方向发展.在各种开发框架之外,打包编译等技术也是层出不穷,开发体验也是越来越好.例如HMR,让我们的更新可以即时可见,告别了 ...
- 极简 Node.js 入门 - 3.1 File System API 风格
极简 Node.js 入门系列教程:https://www.yuque.com/sunluyong/node 本文更佳阅读体验:https://www.yuque.com/sunluyong/node ...
- 解决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 ...
- node.js & create file
node.js & create file node js create file if not exists https://nodejs.org/api/fs.html#fs_fs_ope ...
随机推荐
- cc 攻击
糟糕,系统又被攻击了 原创 二马读书 二马读书 10月16日 几年前,我们的电商平台遭遇过一次CC攻击(一种分布式网络攻击,后面有详细介绍
- (转载)微软数据挖掘算法:Microsoft 时序算法之结果预测及其彩票预测(6)
前言 本篇我们将总结的算法为Microsoft时序算法的结果预测值,是上一篇文章微软数据挖掘算法:Microsoft 时序算法(5)的一个总结,上一篇我们已经基于微软案例数据库的销售历史信息表,利用M ...
- xftp 提示无法显示远程文件夹
在用xftp远程服务器,打开文件夹的时候一直提示"无法显示远程文件夹" 解决方案: 1.网上大多解决方案是文件->属性->选项->将使用被动模式选项去掉即可 2. ...
- XCTF-你是谁
前期工作 查壳,无.运行 不知道有啥用,迷宫题? 逆向分析 文件结构 查看了一下主要逻辑在background中,因为MainActivity的setContentView是background.ba ...
- 设计模式c++(4)——装饰者模式
装饰者模式动态地将责任附加到对象上.若要扩展功能,装饰者提供了比继承更有弹性的替代方案. 装饰者模式的整体思路比较简单,就是在类的实例中包含一个同类型的成员变量,然后用实例来装饰该成员变量.这样就就可 ...
- Java8 ,LocalDate,LocalDateTime处理日期和时间工具类,
Java8 ,LocalDate,LocalDateTime处理日期和时间工具类 1.获取今天的日期 2.在Java 8 中获取年.月.日信息 3.在Java 8 中处理特定日期 4.在Java 8 ...
- Spring cloud-Bus (消息总线)
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring ...
- samba 随笔
SElinux以及防火墙的关闭 关闭SELinux的方法: 修改/etc/selinux/config文件中的SELINUX="" 为 disabled ,然后重启. 如果不想重启 ...
- ElasticSearch 安装与运行
公号:码农充电站pro 主页:https://codeshellme.github.io 本节来介绍 ES 的安装. 1,下载 ES ES 是基于 Java 语言开发的,因此,要安装 ES,首先需要有 ...
- 校招有感:计算机专业毕业生如何找工作(Java方向)
这两年毕业生的就业形势,大家都看在眼里.由于我平时本职会到校园去做校招,兼职会做培训,所以也接触到了不少计算机应届毕业生. 我看到的计算机专业毕业生,各种情况的都有.虽然其中有学校的因素,但学校的因素 ...