Site icon RemoteStack

How to Rename All Files within Folder in Node Js

You often change image file names; sometimes, you change one image name and multiple image names.

What if you have to change all the file names altogether? In the Node js application changing all files, the name is super easy.

In this tutorial, we will show you how to rename all files names in a folder or directory within the Node js application. To change the mass image names in a folder, we will use the node’s readdir() method and a for a loop.

Node js is a powerful yet profound tool; it is a javascript runtime environment powered by a V8 engine. It is ideally used to execute the JavaScript code outside a web browser.

This tutorial will touch upon all the aspects which are essential for a beginner developer. We will cover everything, even for this tiny functionality. Whether it is about setting up a node app from scratch using npm or creating a primary node file, just rest assured!

Node Js Change All Files Name in Folder Example

Create Project Directory

The very first step is to create a project directory, this will be the locus of all the files and folder that are essential for node application development.

Now, head over to terminal, write the eloquent mkdir command.

When you execute this command, it will manifest a new folder on your machine.

mkdir node-man

Once, the folder is created, gradually move inside the project folder.

cd node-man

Please note, you can give any name to your project.

Initialize Node App

In the next step, we will check out how to use the npm init command to form the basis of node project.

npm init

As soon as you execute the initializer command, some questions manifest on your terminal screen. Answer, them accordingly, or if you want to make it faster, just press enter unless the questions are completed.

Now, the above command created a new package.json file.

A pacakage.json file is a heart of node project, this file contains the information about the node project, scripts and packages etc.

Create index.js File

Our primary task is to rename images’s name within a specific folder, make sure to create `img` directory at the root of your node app.

Also, do not forget to keep few image files inside the directory.

After that, at the root of your app. Make sure to create the index.js file.

After the file is created, copy all the given code and paste it inside the index.js file.

const fs = require('fs')

const directory = './img/'

fs.readdir(directory, (error, files) => {
  if (error) {
    throw error
  }

  for (let file of files) {
    let imgFile = directory + file
    let renameFile = directory + 'New_File_Name-' + file

    fs.rename(imgFile, renameFile, function (error) {
      if (error) {
        console.log('ERROR: ' + error)
      }

      console.log(renameFile + ' : File name successfully changed.')
    })
  }
})

Run Node Project

We have successfully created the node app, the final task is to run the node app.

Running a node app is not tuff, go to the console and use the given command to execute the code inside the main server file.

node index.js

As you can see below, we have changed the names of the files located in the `img` direcotry.

./img/New_File_Name-star-trek.jpeg : File name successfully changed.

./img/New_File_Name-yoda.jpeg : File name successfully changed.

Summary

In web development, nothing is absolute. Every time we finish learning something new, from another direction, suddenly something new arrives. Nevertheless, we always try to cover new concepts in app development.

To follow the same tradition we today, we talked about renaming all files in the node app within a specific folder.

We assume you will love our quick tutorial and share your feedback with us.

Exit mobile version