wsw-copydir

1.0.1 • Public • Published

复制文件夹及其内容

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.readdirSync(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( "aa","copy-wsw");
module.exports = copyDir;

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i wsw-copydir

      Weekly Downloads

      1

      Version

      1.0.1

      License

      ISC

      Unpacked Size

      4.5 MB

      Total Files

      1413

      Last publish

      Collaborators

      • wsw1976017366