0

I am trying to move files from one directory to another using the mv module. The problem is, once the files are moved, the source directory gets deleted. I dont want this, I want only the files that are moved to be deleted from the source directory. The source directory should remain (even if it is empty). Not sure how to do this with the mv module (or if there are any other options).

My code

var pathToPdf = path.join(__dirname, '../pathToPdf/');

` var intermediate = path.join(__dirname, '../intermediate/');

 fs.readdir(pathToPdf, function(err, files) {
    if (err) return;

    files.forEach(function(file){

        mv(pathToPdf, intermediate, function(err) {
            if(err){
                console.log("oops!")
            }
        });
       ----move code --- 

This code is moving the files to intermediate directory, but the pathToPdf directory gets deleted, which I want to avoid. Please advise.

4
  • Please show the code for mv(). Commented Sep 16, 2015 at 6:02
  • var mv = require('mv'); npmjs.com/package/mv Commented Sep 16, 2015 at 6:11
  • If the source and dest are on the same volume, then the source directory is just renamed to the destination location, thus the source is no longer present. That's how mv() is written to work. Also, why are you doing a files.forEach() and then not using the file you are iterating? Your code looks wrong in that regard. Commented Sep 16, 2015 at 6:19
  • Thanks! I was able to resolve this. Since it is production code, I cannot paste in the whole code: but I got it to work as I wanted. Snippet below: files.forEach(function(file){ mv(pathToPdf+file, intermediate+file, function(err) { if(err){ console.log("oops!") } }); Commented Sep 16, 2015 at 6:46

1 Answer 1

1
 files.forEach(function(file){
           console.log(file)
            console.log("pathToPdf", pathToPdf+file)
            mv(pathToPdf+file, intermediate+file, function(err) {
                if(err){
                    console.log("oops!")
                }
            });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.