2

I am using this npm to create directory

var mkdirp = require('mkdirp');
var dest = denotes the path
mkdirp(dest, function (err) {
    if (err) cb(err, dest);
    else cb(null, dest);
});

My doubt is how do I create two directory in different path?

I have tried following code:

var dest = first path;
var dest2 = second path;
mkdirp(dest,dest2, function (err) {
  if (err) cb(err, dest);
  else cb(null, dest);
});

But its not working how do I do that?Also, I need to rename for those folder which is in different path at same time.

Updated

var mkdirp = require('mkdirp');
var destArray = [ './root/dest1', './dest2' ]
destArray.map( path => {
  mkdirp(path, function (err) {
    if (err) console.error(err)
    else {
    res.json("ok")
      }
  });
})

I have used this solution, Because of loop am getting can't set headers again

5
  • The documentations says usage: mkdirp [DIR1,DIR2..] {OPTIONS}. Try mkdirp( [dest, dest2] , err => ...) Commented Jul 12, 2018 at 10:30
  • Thanks for your reply, I will try this Commented Jul 12, 2018 at 10:31
  • No its throwing error like path must be a string [ './public/uploads/docs/Vishnu T', './public/uploads/inspection/Vishnu T' ] Commented Jul 12, 2018 at 10:33
  • Show real code var dest = first path; is not real code for instance Commented Jul 12, 2018 at 10:40
  • var dest = './public/uploads/docs/Vishnu T'; Commented Jul 12, 2018 at 10:55

2 Answers 2

1

you can try this

var mkdirp = require('mkdirp');
var destArray = [ './root/dest1', './dest2' ]
destArray.forEach( path => {
  mkdirp(path, function (err) {
    if (err) console.error(err)
    else console.log('Directory created: ' + path )
  });
})
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your reply. Actually am sending json data..So When i am going to create a next path am getting can't set headers error
Why .map? It should be forEach
@JeremyThille Yes, we can use forEach as well
Well in this case, you should use .forEach and not .map because .map is supposed to apply a function to each element in the array and return it, creating a new array, which .forEach does not.
0

You can do this using the following code

const fs = require('fs');
const {promisify} = require('util');
const mkdir = promisify(fs.mkdir);

const destArray = [ 'dest1', 'dest2'];

Promise.all(destArray.map(destPath => mkdir(destPath))).
   then(res => {
       console.log('Directories created');
   }).
   catch(err => {
       console.error(err);
});

4 Comments

can I able to set headers here
@Vishnu what do you imply by setting headers?
like req.flash('add', 'Client added successfully.');
yes you can send it .Inside the then and catch block respectively

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.