删除文件如下:

过程:先判断文件路径是否存在、读取该文件下所有文件、循环该文件,判断是否是文件夹还是文件、

 

移除文件夹使用fs.rmdirSync("路径")

移除文件使用fs.unlinkSync("路径",function(){err})

if (fs.existsSync(pathImg)) {
    files = fs.readdirSync(pathImg);
    files.forEach(function (file, index) {
        var curPath = pathImg + "/" + file;
        if (fs.statSync(curPath).isDirectory()) { // recurse
            fs.rmdirSync(pathImg);
            console.log("文件夹");
        } else { // delete file
            console.log("删除文件",file);
            fs.unlinkSync(curPath,function (err) {
                if (err) throw err;
            });
        }
    });
    // fs.rmdirSync(pathImg);
}

 


 
                    
            
                

相关文章:

  • 2022-12-23
  • 2021-09-14
  • 2022-12-23
  • 2022-12-23
  • 2021-07-16
  • 2021-11-22
  • 2021-10-05
  • 2022-03-04
猜你喜欢
  • 2021-07-25
  • 2021-10-31
  • 2021-08-09
  • 2022-12-23
  • 2021-10-16
相关资源
相似解决方案