a blog for those who code

Tuesday 13 October 2015

How to Add and Remove directories in Node.js

In this post we will be discussing about adding and removing directories in Node.js. This post will be useful if you want to manipulate the structure of your directories by adding and removing directories in Node.js. In our previous post we have discuss about how to get list of files in a directory in Nodejs and how to navigate through directories in Nodejs.

At first we will be discussing about addition of directories. In Node.js it is very easy to add directories by taking the help of file-system module (fs). There are two variants of make directory function in fs module, one is synchronous and other is asynchronous.

For Example : Synchronous Function

var fs = require('fs');

args = process.argv.splice(2);
args.forEach(function(arg){
  try {
    fs.mkdirSync(arg);
  } 
  catch(err) {
    console.log(err);
  }
});

Asynchronous Function

var fs = require('fs');

args = process.argv.splice(2);
args.forEach(function(arg){
  fs.mkdir(arg, function(err) {
    if(err) console.log(err)
  });
});


In the above two example we have created directory using mkdir and mkdirSync based on the parameter passed in the arguments. You can pass any number of arguments (file path), we are removing first two arguments (node and directory.js) and then iterating over the other arguments. If the directory is already present, it will give an error like "[Error : EEXIST, mkdir '']".


To solve this error you can enclose your mkdir code in an if statement like below

if(!fs.lstatSync(arg).isDirectory()){
  fs.mkdir(arg, function(err) {
    if(err) console.log(err)
  });
}

In the above code we are checking if the path is a directory or not, if the directory is present fs.lstatSync(arg).isDirectory() will return true.

Now we will be discussing about removing of directories. In Node.js it is very easy to remove directories by taking the help of file-system module (fs). There are two variants of remove directory function in fs module, one is synchronous and other is asynchronous.

For Example : Synchronous Function 

var fs = require('fs');

args = process.argv.splice(2);
args.forEach(function(arg){
  try {
    fs.rmdirSync(arg);
  } 
  catch(err) {
    console.log(err);
  }
});

Asynchronous Function

var fs = require('fs');

args = process.argv.splice(2);
args.forEach(function(arg){
  fs.rmdir(arg, function(err) {
    if(err) console.log(err)
  });
});

In the above two example we have removed directory using rmdir and rmdirSync based on the parameter passed in the arguments. If the directory is not present, it will give an error like "[Error : ENOENT, rmdir '']". To solve this problem you can use the command  fs.lstatSync(arg).isDirectory()to check directory is present or not.


If the directory is not empty it will give an error like "[Error : ENOTEMPTY, rmdir '']". To solve this error at first check files are there in a directory or not using this post - How to get list of files in a directory in Nodejs and if the directory is not empty move the files to some other location and delete the directory.


Please Like and Share the CodingDefined Blog, if you find it interesting and helpful.

No comments:

Post a Comment