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. ShardingSphere内核原理 原创 鸽子 架构漫谈 2021-01-09

    ShardingSphere内核原理 原创 鸽子 架构漫谈 2021-01-09

  2. Service Mesh 在百度网盘数万后端的落地实践

    https://www.infoq.cn/article/vpa6aypkizuqiijzflih

  3. Redis-第六章节-事务

    目录 简介 执行过程 特点 案例 watch 简介 事务(Transaction),一般是指要做的或所做的事情.在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序执行单元(unit). 执行 ...

  4. 第八届“图灵杯”NEUQ-ACM程序设计竞赛个人赛(同步赛)

    传送门 B-小宝的幸运数组 题目描述 对于小宝来说,如果一个数组的总和能够整除他的幸运数字k,就是他的幸运数组,而其他数组小宝都很讨厌.现在有一个长度为n的数组,小宝想知道这个数组的子数组中,最长的幸 ...

  5. hbuilder使用技巧总结

    HBuilder是DCloud(数字天堂)推出的一款支持HTML5的Web开发IDE.HBuilder的编写用到了Java.C.Web和Ruby.HBuilder本身主体是由Java编写,它基于Ecl ...

  6. python 拼接字

    在编译脚本的时候,由于脚本的框架是统一写好的,于是乎用上了拼接字的功能, 本脚本实现的是波特率设置的自动化,利用的是正则表达式,TASK函数是统一写好的,此处只做调用 from Args import ...

  7. XCTF-你是谁

    前期工作 查壳,无.运行 不知道有啥用,迷宫题? 逆向分析 文件结构 查看了一下主要逻辑在background中,因为MainActivity的setContentView是background.ba ...

  8. mysql在8.0版本下修改密码的命令

    ubuntu20.04上边部署了一个新的mysql服务,默认没密码,想要修改密码. 使用如下手段 第一种:ALTER USER 'root'@'localhost' IDENTIFIED BY '密码 ...

  9. Hive 使用总结

    1 带分区列的表更改列类型 常见的一个场景是Hive里面一个带分区的表,原来是int类型的字段,后来发现数据超过了int的最大值,要改成bigint.或者是bigint要改string或decimal ...

  10. 一文弄懂-《Scalable IO In Java》

    目录 一. <Scalable IO In Java> 是什么? 二. IO架构的演变历程 1. Classic Service Designs 经典服务模型 2. Event-drive ...