Site icon ParallelCodes

Node.js – How to rename a Directory | Folder

In this tutorial, we will learn how to rename a directory in Node.js. Node.js provides a built-in module named fs which helps in working with file system. We will use fs module to rename the directory or a folder.

fs.rename() function:

fs.rename() function is a asynchronous function. This function accepts 3 parameters oldPath, newPath and a callback function. As the name suggest oldPath is the folder name which you wish to change with newPath i.e. new folder name.

Example:

const fs = require("fs")

let folderName = './media/New folder';
let newFolderName = './media/videos';

fs.rename(folderName, newFolderName, function(err) {
if (err) {
console.log(err)
} else {
console.log("Directory rename successful");
}
})

This will rename folder inside the media folder with name New Folder to videos.

Output:

Directory rename successful

Error Output:

[Error: ENOENT: no such file or directory, rename 'C:\Code\Node\fileSystem\media\New folder' -> 'C:\Code\Node\fileSystem\media\videos'] {
errno: -4058,
code: 'ENOENT',
syscall: 'rename',
path: 'C:\\Code\\Node\\fileSystem\\media\\New folder',
dest: 'C:\\Code\\Node\\fileSystem\\media\\videos'
}

Code Snapshot:

DOWNLOAD SOURCE CODE

There is another way to rename a directory in Node.js. This is a synchronous method meaning the code execution will be blocked until the current function runs successfully or throws an error.

fs.renameSync():

fs module provides function fs.renameSync(). This function will rename a directory or a folder in a synchronous manner. This method accepts only two parameters: oldPath & newPath and does not have any callback function to return the result.

Example:

const fs = require("fs")

let folderName = './media/videos';
let newFolderName = './media/css';

try {
fs.renameSync(folderName, newFolderName);
console.log("Directory rename successful");
} catch (err) {
console.log(err);
}

Output:

Directory rename successful

Snapshot:

Both the above codes will throw an error in the file directory already exists. So it’s better to first check if the new folder name or a directory already exists at the specified file path.

The fs.existsSync() helps is checking if the specified folder already exists or not.

Example:

const fs = require("fs")
let folderName = './media/New folder';
let newFolderName = './media/videos';

if(fs.existsSync(newFolderName))
{
console.log("Folder already exists");
return;
}
else{
fs.rename(folderName, newFolderName, function(err) {

if (err) {
console.log(err);
return;
}
console.log("Directory rename successful");
})
}

This will first check for the newFolderName existence and prevent the else condition i.e. directory rename function to execute if it already exists.

DOWNLOAD SOURCE CODE

We have also made a tutorial on:

How to create directory using Node.js

Node.js – Create, read and write text files.

HTTP module in Node.js

Install Node.js on Windows.

Node.js – How to create PDF documents.


1
Exit mobile version