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 ...
随机推荐
- GDB 简单学习
一般来说,GDB主要帮忙你完成下面四个方面的功能: 1.启动你的程序,可以按照你的自定义的要求随心所欲的运行程序. 2.可让被调试的程序在你所指定的调置的断点处停住.(断点可以是条 ...
- 使用Robo 3T访问MongoDB数据库
使用Robo 3T操作MongoDB数据库教程:https://blog.csdn.net/baidu_39298625/article/details/99654596 在IDEA中用三个jar包链 ...
- Linux环境ZooKeeper安装配置及使用
Linux环境ZooKeeper安装配置及使用 一.ZooKeeper 1.1 zookeeper作用 1.2 zookeeper角色 1.3 zookeeper功能 二.集群规划 三.安装流程 (1 ...
- jQuery操作CheckBox的方法(选中,取消,取值)详解
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...
- BGP总结(二)
BGP属性 路由器发送关于目标网络的BGP更新消息,更新的度量值被称为路径属性.属性可以是公认的或可选的.强制的或自由决定的.传递的或非传递的.属性也可以是部分的.并非组织的和有组合都是合法的,路径属 ...
- 基于Qt的tcp客户端和服务器实现摄像头帧数据处理(客户端部分)
项目简述 实现客户端调用摄像头,并以帧的形式将每一帧传输到服务端,服务端将图片进行某些处理后再返回给客户端.(客户端与服务端通信代码部分参考<Qt5 开发及实例>) 项目步骤 客户端的编写 ...
- CF - 392 C. Yet Another Number Sequence (矩阵快速幂)
CF - 392 C. Yet Another Number Sequence 题目传送门 这个题看了十几分钟直接看题解了,然后恍然大悟,发现纸笔难于描述于是乎用Tex把初始矩阵以及转移矩阵都敲了出来 ...
- Grakn Forces 2020
比赛链接:https://codeforces.com/contest/1408 A. Circle Coloring 题意 给出三个长为 $n$ 的序列 $a,b,c$,对于每个 $i$,$a_i ...
- C# 替换文件名的字符
https://www.cnblogs.com/lindexi/p/8970466.html
- conda 命令笔记
1.虚拟环境 conda -V # 查看当前conda 版本 conda update conda # 更新conda conda env list 查看当前已有的虚拟环境 conda create ...