nodejs削除フォルダとファイル(全)
813 ワード
var fs = require('fs')
/**
*
* @param {*} path
* @param {*} reservePath path path reservePath
*/
function delFile(path, reservePath) {
if (fs.existsSync(path)) {
if (fs.statSync(path).isDirectory()) {
let files = fs.readdirSync(path);
files.forEach((file, index) => {
let currentPath = path + "/" + file;
if (fs.statSync(currentPath).isDirectory()) {
delFile(currentPath, reservePath);
} else {
fs.unlinkSync(currentPath);
}
});
if (path != reservePath) {
fs.rmdirSync(path);
}
} else {
fs.unlinkSync(path);
}
}
}