AI Workshop: learn to build apps with AI →
Working with Files: How to mass rename files in Node.js

Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.


This example shows how to rename a set of files (the same approach works for moving files, since renaming changes the path).

One use case: in static site generators like Hugo, posts can be single files:

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

Using a folder per post makes it easier to add images. Converting many single-file posts to folders by hand is tedious; the following script automates it.

Start by requiring the fs core module (no need to install it):

const fs = require('fs')

Get a reference to the current folder (run the script from the folder you want to change). __dirname is the path of the directory containing the current script.

List all files and folders:

const files = fs.readdirSync(__dirname)

Filter to only .md files:

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

For each filename, create a folder (name without .md) and move the file into it:

fs.mkdirSync(__dirname + '/' + file.replace('.md', ''), { recursive: true })

Then call fs.renameSync() to move the file. The synchronous methods mkdirSync() and renameSync() ensure the folder exists before the file is moved; otherwise the second call could run before the first finishes. (You could use promisify with the async versions; the sync versions keep this example simple.)

fs.renameSync() takes two arguments:

  1. the current path
  2. the path we want to move to

The current path is:

__dirname + '/' + file

The path we want to move to is:

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

Create a folder named after the file (without .md), then move the file into it as index.md:

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

Full script:

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 })
    fs.renameSync(
      __dirname + '/' + file,
      __dirname + '/' + file.replace('.md', '') + '/index.md'
    )
  }
}

Lessons in this unit:

0: Introduction
1: How to check if a file exists in Node.js
2: How to create an empty file in Node.js
3: How to get the names of all the files in a folder in Node
4: How to get the last updated date of a file using Node.js
5: How to remove a file with Node.js
6: How to write a JSON object to file in Node.js
7: How to empty a folder in Node.js
8: Working with file descriptors in Node
9: Node File Paths
10: Node file stats
11: Working with folders in Node
12: How to use the Node.js fs module with async/await
13: How to get the current folder in Node
14: How to get the file extension in Node.js from the MIME type
15: How to get the file extension in Node.js
16: How to list files in a folder in Node
17: ▶︎ How to mass rename files in Node.js
18: Node.js get all files in a folder recursively
19: Incrementing multiple folders numbers at once using Node.js
20: Save some text to a file in Node.js