Javascript——文件夹拷贝

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);
});

 

你可能感兴趣的:(Javascript——文件夹拷贝)