创建文件夹的例子
const fs = require('fs')
const path = require('path');
try {
// 创建文件夹
fs.mkdirSync('./abc')
} catch (err) {
//
}
const paths = fs.readdirSync('./abc'); // 同步读取当前目录
paths.forEach(item => {
console.log(item)
// eslint-disable-next-line
const __src = path.resolve(__dirname, './abc', item)
console.log(__src)
fs.stat(__src, function(err, stats) {
if (err) {
console.log(err)
}
if (stats.isFile()) { //如果是个文件则拷贝
console.log('文件')
} else {
console.log('文件夹')
}
})
})
删除文件的例子
const fs = require('fs')
const path = require('path');
// eslint-disable-next-line
deleteFolder(path.resolve(__dirname, './abc'));
function deleteFolder (path) {
let files = [];
if (fs.existsSync(path)) {
files = fs.readdirSync(path);
files.forEach(function(file) {
let curPath = path + '/' + file;
if (fs.statSync(curPath).isDirectory()) {
deleteFolder(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}