ItSolutionStuff.com

Node JS Rename All Files in Folder Example

By Hardik Savani • September 20, 2021
Node JS

Hello all! In this article, we will talk about rename all files in folder. i would like to share with you node js rename multiple files. it's simple example of node js rename files in folder. This tutorial will give you simple example of how to rename all files in a folder using node.js.

We will use fs npm package for rename all files in folder using node.js. fs package provide rename() for renaming all files. let's see simple example

Step 1: Create Node App

run bellow command and create node app.

mkdir my-app

cd my-app

npm init

Step 2: Create server.js file

Make sure, you have "uploads" folder with some files.

server.js

const fs = require('fs');

var folder = './uploads/';

fs.readdir(folder, (err, files) => {

if (err) throw err;

for (const file of files) {

var filePath = folder+file;

var filePathRename = folder + 'RENAME-' + file;

fs.rename(filePath, filePathRename, function(err) {

if ( err ) console.log('ERROR: ' + err);

console.log(filePathRename + ' :File Rename Successfully.');

});

}

});

now you can simply run by following command:

node server.js

Output:

i hope it can help you...

Tags: Node JS
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Delete Directory in Node.js?

Read Now →

Node JS - How to Delete All Files in Directory?

Read Now →

How to Check File is Exist or Not in Node JS?

Read Now →

How to Create Directory if does not Exists in Node JS?

Read Now →

How to Delete File If Exists in Node JS?

Read Now →

How to Get All Files from Folder in Node JS?

Read Now →

Node JS Express Route with Parameters Example

Read Now →

How to Push Element in Array in Node JS?

Read Now →

Node JS Foreach Loop Array Example

Read Now →

Axios HTTP requests in Node JS Example

Read Now →