注意:在win10,v10.16.1 环境运行无问题
首先引入相关包(会在使用处具体说明):
const fs = require('fs')const path = require('path')const child_process = require('child_process')const fsEx = require('fs-extra')/** * @des 该包为实验性API */const fsPromises = require('fs').promises
对文件的操作
复制文件
这里列出三种方式:
其中的同步或异步方法可酌情更改,实现代码如下
/** * @param { copiedPath: String } (被复制文件的地址,相对地址) * @param { resultPath: String } (放置复制文件的地址,相对地址) */function copyFile(copiedPath, resultPath) { copiedPath = path.join(__dirname, copiedPath) resultPath = path.join(__dirname, resultPath) try { /** * @des 方式一 */ // fs.writeFileSync(resultPath, fs.readFileSync(copiedPath)) /** * @des 方式二 */ // fs.copyFileSync(copiedPath, resultPath) console.log('success'); } catch (error) { console.log(error); } /** * @des 方式三 */ fsPromises.copyFile(copiedPath, resultPath) .then(() => { console.log('success'); }).catch((err) => { console.log(err); });}
删除文件
使用 unlinkSync 方法,实现代码如下
/** * @param { delPath:String } (需要删除文件的地址) * @param { direct:Boolean } (是否需要处理地址) */function deleteFile(delPath, direct) { delPath = direct ? delPath : path.join(__dirname, delPath) try { /** * @des 判断文件或文件夹是否存在 */ if (fs.existsSync(delPath)) { fs.unlinkSync(delPath); } else { console.log('inexistence path:', delPath); } } catch (error) { console.log('del error', error); }}
对文件夹(目录)的操作
以下代码有引用,复制文件相关方法
复制文件夹
使用了两种方式:
child_process 递归的读取文件和文件夹再在指定地址创建实现代码和释意如下:
/** * @des 参数解释同上 */function copyFolder(copiedPath, resultPath, direct) { if(!direct) { copiedPath = path.join(__dirname, copiedPath) resultPath = path.join(__dirname, resultPath) } function createDir (dirPath) { fs.mkdirSync(dirPath) } if (fs.existsSync(copiedPath)) { createDir(resultPath) /** * @des 方式一:利用子进程操作命令行方式 */ // child_process.spawn('cp', ['-r', copiedPath, resultPath]) /** * @des 方式二: */ const files = fs.readdirSync(copiedPath, { withFileTypes: true }); for (let i = 0; i < files.length; i++) { const cf = files[i] const ccp = path.join(copiedPath, cf.name) const crp = path.join(resultPath, cf.name) if (cf.isFile()) { /** * @des 创建文件,使用流的形式可以读写大文件 */ const readStream = fs.createReadStream(ccp) const writeStream = fs.createWriteStream(crp) readStream.pipe(writeStream) } else { try { /** * @des 判断读(R_OK | W_OK)写权限 */ fs.accessSync(path.join(crp, '..'), fs.constants.W_OK) copyFolder(ccp, crp, true) } catch (error) { console.log('folder write error:', error); } } } } else { console.log('do not exist path: ', copiedPath); }}
新闻热点
疑难解答
图片精选