How I Solved a Folder Management Problem

Recently, I encountered a problem while creating a large number of folders. Each folder was formatted with a number followed by a dash and a string, like this:

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

Later, I realized that I needed to insert a new folder in the middle, like this:

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

The challenge arose when I had to change the numbers of all subsequent folders to reflect the insertion of the 3-NEWONE folder. To automate this process and avoid manual effort in the future, I decided to create a Node.js command line app called increment.js.

To start, I added a command line argument to specify the number from which I wanted the folders to be incremented. For example:

node rename.js 4

To retrieve the command line argument in Node.js, I used process.argv:

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

If no number is provided as an argument, the program will display an error message and terminate:

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

Now that I had the starting number, the next step was to retrieve the names of the folders that needed to be incremented. Since the script was placed in the same folder as the subfolders, I could simply read from ./, which refers to the current folder. Here’s how I retrieved the names of all files and subfolders in the current folder:

const fs = require('fs');

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

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

To ensure that only folders were retrieved, I added a filter to exclude files.

Next, I iterated through the list of folders:

folders.map((folder) => {

});

Within the iteration, I extracted the number from each folder name:

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

If there was a match (indicating that the folder had a number in its name), I extracted the number and converted it from a string to a number:

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

Finally, if the extracted number was higher than or equal to the starting number argument, I renamed the folder by 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));
   }
 }
});

And that’s it! The above code represents the final source code of the 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));
   }
 }
});

Tags: node.js, folder management, command line app, automation