Mass Renaming Files with Node JS
Tags: javascript, node
Published: 2020-12-09 22:38:10

We've all been there, when you have a ridiculous amount of files you have to rename, and doing them one by one sounds like mundane pain. Maybe you're back in 2007 - the days of downloading hundreds of .mp3 files from sketchy websites only to have them all be name formatted like `WebsiteName_SongName_Artist(1).mp3`. You don't want that "Websitename" in there, nor do you want that extra "(1)" at the end, but since you downloaded Pavement's entire discography there's a lot to go through, and you don't want to do them all at once. This is a simple script to get you through them quickly.

I'm using the script for modern day reasons, of upgrading some code from AngularJS to Angular 2+ and trying to follow the naming conventions of the [style guide](https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md).

## Mass Renaming Files in the Same Directory/Folder

First thing first is setting up your file with all the functionality it needs. You can just make a new .js file in notepad++, or whatever you like to use, and save it where ever you like to save things. 

```
const { join } = require('path');
const { readdirSync, renameSync } = require('fs');
var fs = require('fs');

const current = ".controller.js";
const future = ".component.ts";
```

In here we are grabbing the functionality to read from directories, as well as sync the renamed files back to those directories. We are also declaring that `current` file name part we are looking for, and what we are making to change it to in the `future`. 

Next we are going to be declaring the path to whatever folder contains the files we want to change, as well as reading that directory and assigning everything that we found out to a const named files.

```
const pathName = 'C:/myDevelopment/myApp/directoryToChange/';
const files = readdirSync(pathName);
``` 
###Looping through files

I like to start off with a console.log at the beginning so that I'll be able to double check what I'm changing things to. Below that we are looping through all the items in files and checking if it ends with what we have assigned as `current`. If it does then it'll replace that ending with the one I want in the `future` and sync that to the original folder.

```
console.log("Here we go!", current, future);

files
  .forEach(file => {
    if (file.endsWith(current)) {
     
      const newFilePath = join(pathName, file.replace(current,future));
      const filePath = join(pathName, file);

      console.log(newFilePath, file);
      renameSync(filePath, newFilePath);
    }}
)
```

Depending on how many files you have, and in how many directories you have them could be worth it to do recursion and a tree search instead of this straight forward solution. But if you know roughly how many directories to go through, you can just amend this program to loop twice. 

###Directory full of Directories

Lets say we are looping through a directory that is full of directories of Pavement's albums. Pavement was fairly prolific in albums and EPs, so it would take a while to run this program against each individual directory. Instead we have have a loop within a loop, that checks to see if the instance it is on is a file or a directory itself, and if it is a directory itself it loops again with that directory. This way you can run the program against your Pavement folder that contains all the folders for the Albums, which then contains the .mp3 files you want to mass update.

```
files
  .forEach(file => {
    if (file.endsWith(current)) {

      const newFilePath = join(pathName, file.replace(current,future));
      const filePath = join(pathName, file);

      console.log(newFilePath, file);
      renameSync(filePath, newFilePath);

    }

    else if (fs.statSync(pathName + file).isDirectory()) {

      var folder1 = readdirSync(pathName + file);
      folder1.forEach(file1 => {
        if (file1.endsWith(current)) {
  
          const filePath = join(pathName + file, file1);

          const newFilePath = join(pathName + file, file1.replace(current,future));

          renameSync(filePath, newFilePath);

          console.log(newFilePath, file1);

        }
```

In this instance, we check with `fs` if the file at that path is a directory, and if it is we go inside to run another loop on that code.

Once you're all set and ready run this code, just open your terminal and `cd` to where this file is. Once there simply running `node file_name.js` will kick off the file. With all the console logs you can follow along with the files that it has changed for you.

With all the time this program has saved you, you can organize even more of your audio library.