1.0.0 • Published 4 months ago

yyq-demo v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
4 months ago

#判断文件在创建文件夹

const fs=require('fs');
const copy=(sourcePath,targetPath)=>{
    const flay1=fs.existsSync(sourcePath);
    const flay2=fs.existsSync(targetPath);
    //容错
    if(!flay1){
        throw new Error('源文件不存在'+sourcePath);
        return;
    }
    if(flay2){
        throw new Error('目标文件存在'+targetPath);
        return;
    }
    //创建目标文件夹
    fs.mkdirSync(targetPath);
    //读取源目录的子目录
    fs.readdirSync(sourcePath).forEach(item=>{
        //拼接
        let a=sourcePath+"/"+item;
        let b=targetPath+"/"+item;
        //读取信息
        if(fs.statSync(a).isFile()){
            //是文件
            fs.copyFileSync(a,b);
        }else{
            //是文件夹(递归)
            copy(a,b)
        }
    })
}
copy('zi',"aa")
1.0.0

4 months ago