Read and rename files in a directory with Nodejs fs package
Tags: javascript, node, tutorial, beginners
Published: 2022-02-03 11:19:15

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](https://dev.to/emkay860/working-with-nodejs-file-system-and-path-module-1anj)

## 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](https://dev.to/emkay860/working-with-nodejs-file-system-and-path-module-1anj) that was generated using a plain create-react-app template.


![File structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n3swnszzb0hwxgoek8md.png)

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`.

```js
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](https://dev.to/emkay860/working-with-nodejs-file-system-and-path-module-1anj). This method returns a string array of the files in the directory

```js
// 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`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) to iteraate through the array, get each file and append the index of that file to it's filename.

```js
// 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.

```js
...
  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

```js
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

```js
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:

```js
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](https://nodejs.dev/learn).
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