node递归删除文件

node递归删除文件


/*jshint esversion: 6 */

const fs = require('fs');
const path = require('path');
const join = path.join;


function walk(dir,suffix='.iml') {

    var result = [];
    var list = fs.readdirSync(dir);
    list.forEach((file) => {
        var currentPath = join(dir, file);
        var stat = fs.statSync(currentPath);
        if (stat && stat.isDirectory()) {
            result = result.concat(walk(currentPath,suffix));
        } else {
            if (path.extname(currentPath) === suffix) {
                result.push(currentPath);
            }
        }
    });
    return result;
}

const basePath = path.resolve('E:/code');
const list = walk(basePath);
list && list.forEach((file) => {
    //删除文件
    fs.unlinkSync(file);
});

发布了76 篇原创文章 · 获赞 66 · 访问量 51万+

猜你喜欢

转载自blog.csdn.net/u013887008/article/details/103846066