Ever need to a search and replace with a folder of filenames? You saved a bunch of images from your camera as Steve and Michelle Wedding when you meant Stephen and Michelle? Or you put the wrong date or otherwise need to change a lot of filenames and don’t feel like doing it manually?

Fortunately you can use javascript to do the work for you. Javascript is very versatile and perfect for these quick and dirty solutions where you don’t need something complex, just a little script that goes into a directory and replaces all instances of one word, or string, with another. I adapted this script from this snippet.

In this example, I’m going to rename all the files that end in .gif.png so that they end in just .gif. The code will replace all instances of .gif.png, even if they appear in the middle of a filename (so image.gif.png.jpg will be renamed image.gif.jpg), but I know that in my directory this won’t happen because of the files I have inside of it.

Again, this is a quick and dirty script, no error checking and is not a robust solution. Remember to make your edits to the values before you run it.

var sFolderName, sStringToFind;
var nResult;
//////////////////////////////////////////
// Set these values
sFolderName = "C:\\Temp\\Images"; // use directory containing image
sStringToFind = ".gif.png";
sStringToReplace = ".gif";
//////////////////////////////////////////
 
nResult = renameFiles(sFolderName, sStringToFind, sStringToReplace);
WScript.Echo(nResult + " files renamed");
 
//    Function Name:    renameFiles
//    Parameters:
//    sFolder:    Folder Name (use double backslashes)
//    sString1:    String to search for
//    sString2:    String to replace
//    Returns:    Number of files renamed
 
function renameFiles(sFolder, sString1, sString2) {
    var oFSO, oFile, oFolder;
    var re, index;
    var sName;
    var i = 0, n;
 
    oFSO = new ActiveXObject("Scripting.FileSystemObject");
    oFolder = oFSO.GetFolder(sFolder);
    try {
        index = new Enumerator(oFolder.Files);
        for (; !index.atEnd(); index.moveNext()) {
            oFile = index.item();
            sName = oFile.Name;
            n = sName.indexOf(sString1);
            if(n != -1) {
                try {
                    sName = sName.substring(0, n) + sString2 + 
                            sName.substr(n + sString1.length);
                    oFile.Name = sName;
                    i++;
                } catch(e) {
                    WScript.Echo("Can not rename file " + sName + " because\n" + e.description);
                }
            }
        }
    }
    catch(e) {
        WScript.Echo("Could not access folder " + sFolder + " because\n" + e.description);
        return 0;
    } finally {
        oFSO = null;
        re = null;
        return i;
    }
}

There you have it, a quick search and replace javascript for renaming files.

RSS Subscribe Like this article? Get all the latest articles sent to your email for free every day. Just click "Subscribe" and enter your email. Your email will only be used for this daily subscription and you can unsubscribe anytime.

One Response to “Rewrite & Rename Filenames using Javascript”

  1. » YSlow’s Smush.it Adds File Uploader Says:

    […] included an uploader tool. This lets me download all of my image files, smush them in batches, rewrite some filenames, and upload them back to my […]

Leave a Reply