su-fuzhi
v1.0.0
Published
```javascript const fs = require("fs");
Downloads
3
Readme
复制文件夹
const fs = require("fs");
const copyDir = (sourcePath, targetPath) => {
const flag1 = fs.existsSync(sourcePath)
const flag2 = fs.existsSync(targetPath)
//1.容错处理
if (!flag1) {
throw new Error("源目录不存在!!!" + sourcePath)
return;
}
if (flag2) {
throw new Error("目标目录存在!!!" + targetPath)
return;
}
//2.创建目标文件
fs.mkdirSync(targetPath);
//3.读取源目录的子目录
fs.readFileSync(sourcePath).forEach(item => {
//拼接
let midSourcePath = sourcePath + "/" + "item";
let midTargetPath = targetPath + "/" + "item";
//读取信息
if (fs.statSync(midSourcePath).isFile()) {
//是文件
fs.copyFileSync(midSourcePath, midTargetPath)
} else {
//是文件夹(递归)
copyDir(midSourcePath, midTargetPath)
}
})
//copyDir("元目录","新目录")
}