DEV Community

Cover image for Mass Renaming Files with Node JS
Sarah Thompson
Sarah Thompson

Posted on

Mass Renaming Files with Node JS

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.

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";
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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);
    }}
)
Enter fullscreen mode Exit fullscreen mode

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);

        }
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (4)

Collapse
 
raddevus profile image
raddevus • Edited

Have you done any Electron development yet? It helps you build a GUI on top of Node. I've done a bit and it can be really great because you can provide "Windows" on top of your Node functionality.
Here's a snapshot of one I built that also allows Open File/Directory dialog to be opened. snapshot of one I built

Collapse
 
raddevus profile image
raddevus

This is a great idea and a nice implementation and serves as a very nice example of what you can do with Node. Thanks for sharing. Tell me what you think about the Go Language.

Collapse
 
andrewbaisden profile image
Andrew Baisden

Nice guide easy to follow.

Collapse
 
dachak profile image
David Chacon

good job!