node删除本地文件夹 并递归删除所有文件和文件夹

node删除本地文件夹并递归删除所有文件和文件夹

  • 删除本地文件夹 并递归删除所有文件和文件夹
  • 删除文件

删除本地文件夹 并递归删除所有文件和文件夹

export const deleteFolder = (path) => {
    if (fs.existsSync(path)) {
        fs.readdirSync(path).forEach( (file)=> {
            var curPath = path + "\\" + file;
            if (fs.statSync(curPath).isDirectory()) { // recurse
                deleteFolder(curPath);
            } else { // delete file
                fs.unlinkSync(curPath);
            }
        });
        fs.rmdirSync(path);
    }
}

删除文件

import {createWriteStream, ensureDirSync, pathExists, remove, removeSync, unlink} from 'fs-extra';

export const removeFile = (myPath) => {
  try {
    unlink(myPath)
  } catch (e) {
    logger.info(e)
  }
  // 删除未下载完成文件
  try {
    removeSync(myPath)
  } catch (e) {
    logger.info(e)
  }
}

你可能感兴趣的:(node.js,javascript,node.js,javascript)