How to mass rename files in Node.js

Find out how to rename a set of files using Node.js

⭐️ 👀 2023 WEB DEVELOPMENT BOOTCAMP starting in days! Join the waiting list to reserve your spot in my 10-weeks cohort course and learn the fundamentals, HTML, CSS, JS, Tailwind, React, Next.js and much much more! 👀 ⭐️

In this blog post I’m going to explain how to rename a set of files.

The same process works to move files to another folder, because when you rename you rename the path of the file.

The motivation for this task was this: in Hugo we can write blog posts as files, like this:

first-post.md
second-post.md
third-post.md

We can also add them to a folder that contains an index.md file:

first-post/
  > index.md
second-post/
  > index.md
third-post/
  > index.md

The difference is that with folder we can add images and associate them more easily to a blog post.

I could have done the change manually, but I had about 50 files in the folder and I didn’t really want to do that job.

I wanted it to happen automatically.

Let’s start by requiring one core module we’re going to use: fs. As it is a core module, there’s no need to npm install it.

const fs = require('fs')

Then, get a reference to the current folder. I suppose we’re going to run the script in the same folder where we want to perform this change.

__dirname is the variable that always points to the current working folder.

I get a list of all the files and folders:

const files = fs.readdirSync(__dirname)

Then I filter out only the items that end with .md:

for (const file of files) {
  if (file.endsWith('.md')) {
    console.log(file)
  }
}

Once we have the file reference, which represents the filename, we can call fs.mkdirSync() to create the folder:

fs.mkdirSync(
  __dirname + '/' + file.replace('.md', ''),
  { recursive: true },
  (err) => {
    console.log(err)
  }
)

Once we create the folder, we call fs.renameSync().

The reason I us the blocking versions mkdirSync() and renameSync() instead of mkdir() and rename() is that I want to wait until the folder is created before moving the file to the folder. Otherwise we might get errors if the second function runs before the second (I’d use await but the Node.js API does not use promises, we’d have to use promisify but it’s simpler to use the blocking version of those APIs).

The fs.renameSync() function accepts 3 parameters:

  1. the current path
  2. the path we want to move to
  3. an callback fired if there’s an error

The current path is:

__dirname + '/' + file

The path we want to move to is:

__dirname + '/' + file.replace('.md', '') + '/index.md'

See? We create a new folder from the file name, then we append index.md:

fs.renameSync(
  __dirname + '/' + file,
  __dirname + '/' + file.replace('.md', '') + '/index.md',
  err => {
    console.log(err)
  }
)

Here’s the full code:

const fs = require('fs')
const files = fs.readdirSync(__dirname)

for (const file of files) {
  if (file.endsWith('.md')) {
    fs.mkdirSync(
      __dirname + '/' + file.replace('.md', ''),
      { recursive: true },
      (err) => {
        console.log(err)
      }
    )

    fs.renameSync(
      __dirname + '/' + file,
      __dirname + '/' + file.replace('.md', '') + '/index.md',
      (err) => {
        console.log(err)
      }
    )
  }
}

One more thing! ⚠️ ✋

At the end of January I will organize the Web Development Bootcamp.

It's a 10-weeks long cohort online course where I will guide you to becoming a Web Developer.

It's not "just a course". It's a big event I organize once a year.

We'll start from zero, learn the fundamentals of Web Development, HTML CSS, JavaScript, Tailwind, Git, using the command line, VS Code, GitHub, Node.js, we'll then learn React, JSX, how to use PostgreSQL, Astro, Next.js, Prisma, deploying on Netlify/DigitalOcean/Fly/Vercel and much more! 

At the end of the first 10 weeks you'll know how to create web sites and web applications and I'll unlock you the 2nd phase of the Bootcamp: you will get access to a large number of projects exclusive to the Bootcamp graduates, so you can follow my instructions to build things like private areas with authentication, clones of popular sites like Twitter YouTube Reddit, create e-commerce sites, and much much more.

Because once you got the fundamentals, you only learn by working on real, exciting projects.

To find out more, visit bootcamp.dev