0

I have a folder called 'Received' and two more folders called 'Successful' and 'Error'. All new files will be stored in the 'Received' Folder and upon being stored in the said folder, it will be processed by my system. Successfuly parsed files will be moved to the 'Successful' folder, while all files that had issues will be stored in the 'Error' folder.

My main concern is basically moving files between directories.

I have tried this:

// oldPath = Received Folder
// sucsPath = Successful Folder
// failPath = Error Folder

// Checks if Successful or fail. 1 = Success; 0 = Fail
if(STATUS == '1') { // 1 = Success;
  fs.rename(oldPath, sucsPath, function (err) {
    if (err) {
      if (err.code === 'EXDEV') {
        var readStream = fs.createReadStream(oldPath);
        var writeStream = fs.createWriteStream(sucsPath);

        readStream.on('error', callback);
        writeStream.on('error', callback);

        readStream.on('close', function () {
          fs.unlink(oldPath, callback);
        });

        readStream.pipe(writeStream);
      }
      else {
        callback(err);
      }
      return;
    }
    callback();
  });
}
else { // 0 = Fail
  fs.rename(oldPath, failPath, function (err) {
    if (err) {
      if (err.code === 'EXDEV') {
        var readStream = fs.createReadStream(oldPath);
        var writeStream = fs.createWriteStream(failPath);

        readStream.on('error', callback);
        writeStream.on('error', callback);

        readStream.on('close', function () {
          fs.unlink(oldPath, callback);
        });

        readStream.pipe(writeStream);
      }
      else {
        callback(err);
      }
      return;
    }
    callback();
  });
}

But my concern here is that it deletes the original folder and passes all files into the specified folder. I believe the logic in the code is that, it literally renames the file (directory included). I also came across 'await moveFile' but it basically does the same thing.

I just want to move files between directories by simply specifying the file name, the origin of the file, and its destination.

DayIsGreen
  • 255
  • 1
  • 4
  • 16
  • 1
    why not copy the file to the correct directory and remove it from Received afterwards? Copying files in node.js is as simple as: https://stackoverflow.com/a/11295106/7480906 – rufus1530 Aug 22 '18 at 07:20

2 Answers2

0

As mentioned by rufus1530, I used this:

fs.createReadStream(origin).pipe(fs.createWriteStream(destination)); 

Deleting the file would be the next step.

I used this:

fs.unlinkSync(file);
DayIsGreen
  • 255
  • 1
  • 4
  • 16
0

Starting with 8.5 you have fs.copyFile which the easiest way to copy files.

So you would create your own move function, that would first try a rename and then a copy.

const util = require('util')
const copyFile = util.promisify(fs.copyFile)
const rename = util.promisify(fs.rename)
const unlink = util.promisify(fs.unlink)
const path = require('path')    

async function moveFileTo(file, dest) {
  // get the file name of the path
  const fileName = path.basename(file)

  // combine the path of destination directory and the filename
  const destPath = path.join(dest, fileName)

  try {
     await fs.rename(file, destPath)
  } catch( err )  {
     if (err.code === 'EXDEV') {
       // we need to copy if the destination is on another parition
       await copyFile(file, destPath)

       // delete the old file if copying was successful
       await unlink(file)
     } else {
       // re throw the error if it is another error
       throw err
     }
  }
}

Then you could use it that way await moveFileTo('/path/to/file.txt', '/new/path') which will move /path/to/file.txt to /new/path/file.txt.

t.niese
  • 39,256
  • 9
  • 74
  • 101