Working with Files: Incrementing multiple folders numbers at once using Node.js

Join the AI Workshop to learn more about AI and how it can be applied to web development. Next cohort February 1st, 2026

The AI-first Web Development BOOTCAMP cohort starts February 24th, 2026. 10 weeks of intensive training and hands-on projects.


I had a problem.

I was creating a large number of folders formatted like this:

1-yo
2-hey
3-cool
4-hi
5-whatsup

A number followed by a dash, and a string.

I got up to 40 of these, and I realized I had to put one in the middle, like this:

1-yo
2-hey
3-NEWONE
3-cool
4-hi
5-whatsup

The problem was that I had to change all the numbers of the folders that now were supposed to follow the 3-NEWONE folder.

I wanted an end result like this, where all the numbers following the new entry are incremented:

1-yo
2-hey
3-NEWONE
4-cool
5-hi
6-whatsup

I did this manually once, then I realized I was definitely going to repeat this process in the future, so I made a Node.js command line app to automate it.

I called the file increment.js and I decided to take a command line argument to set the number I wanted to start from, like this:

node rename.js 4

Getting the number is simple, we get it from process.argv:

const args = process.argv.slice(2)
const startingNumber = args[0]

If no number is present, we’re going to show an error and end the program:

if (!startingNumber) {
  console.log('Add a number argument')
  return
}

Now that we have this number, we can start getting the folder names we need to increment. The script will be positioned in the same folder that contains all the subfolders, so we can just read from ./, which means this folder.

This is how we can get the names of all the files and subfolders contained in the current folder:

const fs = require('fs')

const folders = fs
  .readdirSync('./')
  .map(fileName => {
    return fileName
  })

Let’s filter this to make sure we only get the folders:

const fs = require('fs')

const isFolder = fileName => {
  return !fs.lstatSync(fileName).isFile()
}

const folders = fs
  .readdirSync('./')
  .map(fileName => {
    return fileName
  })
  .filter(isFolder)

Next we can iterate through the folders list:

folders.map(folder => {

})

I extract the number from the folder:

folders.map(folder => {
  const result = folder.match(/(\d)+/)
})

And if there’s a match (the folder has a number in the name), I extract it and transform it from a string into a number:

folders.map(folder => {
  const result = folder.match(/(\d)+/)
  if (result !== null) {
    const num = parseInt(result[0])
  }
})

Finally, if the number is higher than the one we pass as argument, we rename the folder name incrementing the number:

folders.map(folder => {
  const result = folder.match(/(\d)+/)
  if (result !== null) {
    const num = parseInt(result[0])
    if (num >= startingNumber) {
      fs.renameSync(folder, folder.split(num).join(num + 1))
    }
  }
})

That’s it! Here’s the final source code of our little CLI app:

const fs = require('fs')

const args = process.argv.slice(2)
const startingNumber = args[0]
if (!startingNumber) {
  console.log('Add a number argument')
  return
}

const isFolder = fileName => {
  return !fs.lstatSync(fileName).isFile()
}

const folders = fs
  .readdirSync('./')
  .map(fileName => {
    return fileName
  })
  .filter(isFolder)

folders.map(folder => {
  const result = folder.match(/(\d)+/)
  if (result !== null) {
    const num = parseInt(result[0])
    if (num >= startingNumber) {
      fs.renameSync(folder, folder.split(num).join(num + 1))
    }
  }
})

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