将文件从一个文件夹移动到另一个节点js

时间:2018-03-29 18:10:38

标签: node.js file-io

我正在阅读一个csv文件并创建文件夹并尝试将这些文件移动到该文件夹​​中。但我收到错误。

我正在将文件移到.on('结束',功能(数据)..

Error: EPERM: operation not permitted, rename 'c:\Users\XXXXXX\Desktop\project\Nodejs\Attachments\00P9000001LiuvWEAR' -> 'c:\Users\XXXXXXX\Desktop\project\Nodejs\00190

 var csv = require('fast-csv');
  var fs = require('fs-extra');
  var attachmentIDs;
  var recordId;
  var dir;
  var dir1;
  var stream = fs.createReadStream('./Attachment.csv')
      .pipe(csv({
          headers: true
      }))
      .on('data', function(data) {
          recordId = data.Id;
          attachmentIDs = data.ParentId;
          dir = './' + attachmentIDs + '/';
          dir1 = './' + attachmentIDs + '/' + recordId + '/';
          if (!fs.existsSync(dir)) {
              fs.mkdirSync(dir);
          }
          if (!fs.existsSync(dir1)) {
              fs.mkdirSync(dir1);
          }
      })
      .on('end', function(data) {
          var oldPath = './Attachments/' + recordId;
          var newPath = dir1;
          fs.rename(oldPath, newPath, function(err) {
              if (err) {
                  console.log(err);
              } else {
                  console.log("Files Moved");
              }
          });
      });

我正在阅读的csv文件中的CSV内容

Id             IsDeleted    ParentId
00P9000001B22QqEAJ  0   0019000001rYSf9AAG
00P9000001FDf6uEAD  0   a0f9000000VM4TJAA1
00P9000001FDxFQEA1  0   a0f9000000VM4fqAAD
00P9000001FDxSuEAL  0   a0f9000000VM4g5AAD
00P9000001G7nAnEAJ  0   a0f9000000WQ21hAAD
00P9000001G7nAoEAJ  0   a0f9000000WQ21iAAD
00P9000001HGYppEAH  0   0019000001uJKSUAA4
00P9000001HGZ0xEAH  0   0019000001uJKSUAA4
00P9000001ISO9vEAH  0   0019000001xZCYlAAO
00P9000001ISPbmEAH  0   0019000001xZCYlAAO
00P9000001ISPecEAH  0   0019000001xZCYlAAO
00P9000001ITqDFEA1  0   0019000001xZCYlAAO
00P9000001JODmGEAX  0   0019000001xbZz1AAE
00P9000001KTviTEAT  0   0019000001yZmjcAAC
00P9000001LiuvREAR  0   0019000000ySYCvAAO
00P9000001LiuvWEAR  0   0019000000ySYCvAAO

1 个答案:

答案 0 :(得分:0)

从最外层的范围中删除了recordIdattachmentIDs,因为它们不需要在data个事件中共享,并可能污染dir和{{1}因为异步性。

dir1