Codementor Events

Read and rename files in a directory with Nodejs

Published Feb 03, 2022Last updated Feb 07, 2022
Read and rename files in a directory with Nodejs

Hello, in this post, we will be bulk renaming all the files inside of a directory using the fs module rename method
This a continuation of a possibly long series of posts on the node.js fs and path module.
To get the best out of this post, it is recommended that you follow along from the previous post

Expectations

This post assumes a basic knowledge of JavaScript and Node.js

To get started, we will be continuing with the same folder structure from the previous post that was generated using a plain create-react-app template.

File structure

In our index.js, we need to import the fs and path module, next we define a path to our assets folder, I'll name it folderPath.

const fs = require("fs");
const path = require("path");

const folderPath = "./assets";

Now that we have defined a path to our assets folder, let's get to the interesting part.
Next we read from the assets folder using the readdirSync method discussed in the previous post. This method returns a string array of the files in the directory

// read all files in the directory 
// and save filename to filesArr

let filesArr = fs.readdirSync(folderPath);

Since the readdirSync method returns an array, I'm sure you must have guessed that we will need some kind of loop to access each element in the array. So let's do that, we will use a foreach to iteraate through the array, get each file and append the index of that file to it's filename.

// Loop through array and rename all files

filesArr.forEach((file, index) => { 

});

Next, we will re-create the full path to each file inside the foreach loop, get the extension of the file and the file name without the extension using the join, extname, basename method from the path module.

...
  let fullPath = path.join(folderPath, file);
  let fileExtension = path.extname(file);
  let fileName = path.basename(file, fileExtension);
...

Once we have the filename and extension seperated, we can go ahead and construct the new name for our file

let newFileName = fileName + index + "." + fileExtension;

After that, we can rename our file using fs.renameSync. This method accepts the initial name of the file and the new file name as arguments

try {
  fs.renameSync(fullPath, path.join(folderPath, newFileName));
} catch (error) {
  console.error(error)
}

Here is the complete version of the code we've written so far:

const fs = require("fs");
const path = require("path");

const folderPath = "./assets";

// read all files in the directory
let filesArr = fs.readdirSync(folderPath);

// Loop through array and rename all files 

filesArr.forEach((file, index) => {
  let fullPath = path.join(folderPath, file);
  let fileExtension = path.extname(file);
  let fileName = path.basename(file, fileExtension);

  let newFileName = fileName + index + "." + fileExtension;
try {
  fs.renameSync(fullPath, path.join(folderPath, newFileName));
} catch (error) {
  console.error(error)
}
});

That's it. Now you have can rename a bunch of files in your directory all at once and tweak the above code anyhow you like.

Note

In the above example, it is assumed that we only have files in our assets directory and no sub-directories.

To learn more about fs and path modules, visit the official node.js documentation.
In my next posts, I'll be giving more examples of the fs module methods such as the fs.stats and talk about streams using the fs.createReadStream to read the contents of a file.

If you have suggestions or corrections, don't hesitate to get in touch

Discover and read more posts from Majid Kareem
get started
post commentsBe the first to share your opinion
Show more replies