国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

node刪除、復制文件或文件夾示例代碼

2024-05-06 15:35:57
字體:
來源:轉載
供稿:網友

注意:在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

對文件的操作

復制文件

這里列出三種方式:

    使用 writeFileSync 和 readFileSync 結合 使用 copyFileSync 使用promises的copyFile方法

其中的同步或異步方法可酌情更改,實現代碼如下

/** * @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);  }}            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 彭泽县| 城口县| 永兴县| 沁源县| 红原县| 芜湖市| 栾城县| 丽水市| 正安县| 扎囊县| 玉屏| 桃园市| 开远市| 金沙县| 金华市| 哈巴河县| 宝鸡市| 家居| 芮城县| 平顶山市| 额济纳旗| 平舆县| 滕州市| 凉山| 洱源县| 夹江县| 叶城县| 齐齐哈尔市| 沈丘县| 天全县| 张家港市| 南宁市| 郓城县| 方正县| 柯坪县| 大冶市| 铁力市| 宝坻区| 巢湖市| 铁力市| 三原县|