Node 删除目录

简介: Node 删除目录

深度优先删除

let fs = require('fs');
let path = require('path');
function rmdir(dir,cb) {
  fs.readdir(dir,function (err, files) {
    next(0);
    function next(index) {
      if(index == files.length) {
        return fs.rmdir(dir,cb);
      }
      let newPath = path.join(dir,files[index]);
      fs.stat(newPath,function (err, stats) {
        if(err){
          console.log(err);
        }
        if(stats && stats.isDirectory()){
          rmdir(newPath,()=>next(index+1));
        } else {
          fs.unlink(newPath,function (err) {
            if (err) {
              console.error(err);
            }
            next(index + 1);
          });
        }
      })
    }
  })
}
let dirPath = '/home/w/ImJoyApp/'
rmdir(dirPath, function () {
  if (fs.existsSync(dirPath)) {
    // delete soft links
    fs.readdir(dirPath,function (err, files) {
      if (err) {
        console.log(err)
      } else {
        rmdir(dirPath ,function () {
          console.log('delete success')
        })
      }
    })  
  } else {
    console.log('delete success')  
  }
})
相关文章
|
8月前
|
Kubernetes 容器
k8s指定config文件查看node—2023.02
k8s指定config文件查看node—2023.02
node判断文件夹是否存在,不存在直接创建
node判断文件夹是否存在,不存在直接创建
|
19天前
|
JSON 关系型数据库 MySQL
node笔记_读取目录的文件
node笔记_读取目录的文件
18 1
|
19天前
|
JavaScript
Node fs 创建多层文件夹
Node fs 创建多层文件夹
9 0
|
19天前
|
JavaScript 前端开发 数据库
【Node系列】node中的函数
在Node.js中,函数是一段可重复使用的代码,它可以接受输入(参数),执行一系列操作,并返回一个结果(返回值)。
19 3
|
JavaScript
【node错误】/usr/bin/env: node: No such file or directory
背景 安装了node后,执行npm run xxx的命令的时候,报错,提示如下: /usr/bin/env: node: No such file or directory   步骤 1.
6143 0
|
19天前
Node 创建多级目录
Node 创建多级目录
|
19天前
|
JavaScript 前端开发 Linux
2020你应该知道的Node配置
2020你应该知道的Node配置
53 1
|
12月前
|
开发工具 git
Node常用命令
Node常用命令
55 0
|
JavaScript API
Node第三方包 【node-xlsx】
Node第三方包 【node-xlsx】
178 0