nodejs复制文件

nodejs复制文件

const path = require('path')
const fs = require('fs')
 

const arr = ['PLDO201941060204M05775张晓光',
'PLDO201941060204M04723王兆祎',
'PLDO201941060204M05776魏博晨',
'PLDO201941060204M04351朱治宋',
'PLDO201941060204M06014刘艳芳',
'PLDO201941060204M03948王盈安' 
];

const { COPYFILE_EXCL } = fs.constants;

function callback(err) {
    if (err) throw err;
    console.log('源文件已拷贝到目标文');
}


async function creatfolder(path) {
    const dir = await fs.promises.opendir(path)
    for await (const dirent of dir) {
        const name = dirent.name.slice(0, 22)
        console.log(name)
        for await (const item of arr) {
            if (item.includes(name)) {
                fs.mkdir('./public/' + item, function (err) {

                    if (err) {
                        return console.error(err);

                    } else {

                        fs.copyFile('./public/' + dirent.name, './public/' + item + '/' + dirent.name, callback);

                    }

                })
            }
        }


    }

}
creatfolder('./public')
 
来都来了,留个言吧🙂