Javascript-フォルダコピー
1250 ワード
const fs=require("fs");
const path=require("path");
Old=path.join(__dirname,"old");
New=path.join(__dirname,"new")
function cpdir(dirOld,dirNew){
var p=new Promise(function(resolve, reject) {
var i=0;
fs.readdir(dirNew,function(err,list){
list.forEach(item=>{
if(item=="old"){
reject(" ");
i=i+1;
}
});
if(i==0){
fs.mkdir(path.join(dirNew,"old"),function(err){
resolve(" !");
dirNew=path.join(dirNew,"old");
walkDir(dirOld,dirNew);
});
}
});
function walkDir(dirOld,dirNew){
var list=fs.readdirSync(dirOld);
list.forEach(function(item){
if(fs.statSync(path.join(dirOld,item)).isDirectory()){
fs.mkdirSync(path.join(dirNew,item));
walkDir(path.join(dirOld,item),path.join(dirNew,item));
}else{
fs.copyFile(path.join(dirOld,item),path.join(dirNew,item),function(err){
console.log(err);
});
}
});
}
});
return p;
}
cpdir(Old,New)
.then(function(rs){
console.log(rs);
})
.catch(function(rj){
console.log(rj);
});