Search code examples
renamebatch-processingwshjscriptbatch-rename

JavaScript Batch-Renaming in a Folder


I have pairs of strings that need to be replaced in the file names of a given folder, e.g. BUL to Bg-bg, ENG to En-us, etc. There are 12 such pairs. I have found a script to take care of a single pair of replacement but whenever I am trying to add more arguments, it somehow only loops through one and renames just one matching string. Cannot figure our a simpler code that would accept all replacement string pairs at once without creating a separate loop for each pair. Any ideas would be appreciated.

var sFolderName, sStringToFind;
var nResult;

sFolderName = "E:\\Users\\Username\\Desktop\\Sample"; // directory name
sStringToFind1 = "BUL";
sStringToReplace1 = "Bg-bg";
sStringToFind2 = "ENG";
sStringToReplace2 = "En-us";

nResult = renameFiles(sFolderName, sStringToFind1, sStringToReplace1, sStringToFind2, sStringToReplace2);
WScript.Echo(nResult + " files renamed");

//    Function Name:    renameFiles
//    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, sString3, sString4) {
    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) + sString4 + 
                            sName.substr(n + sString3.length);
                    oFile.Name = sName;
                    i++;
                } catch(e) {
                    WScript.Echo("Can not rename file " + sName + " because\n" + e.description);
                }
            }
        }
                index = new Enumerator(oFolder.Files);
        for (; !index.atEnd(); index.moveNext()) {
            oFile = index.item();
            sName = oFile.Name;
            n = sName.indexOf(sString3);
            if(n != -1) {
                try {
                    sName = sName.substring(0, n) + sString4 + 
                            sName.substr(n + sString3.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;
    }
}

Solution

  • You could use replace() and do all replacements inside the same loop:

    function renameFiles(sFolder, sString1, sString2, sString3, sString4) {
        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;
    
                sName = sName.replace(sString1, sString2);
                sName = sName.replace(sString3, sString4);
    
                if (oFile.Name != sName) {
                    i++;
                    oFile.Name = sName;
                }
            }
        }
        catch(e) {
            WScript.Echo("Could not access folder " + sFolder + " because\n" + e.description);
            return 0;
        } finally {
            oFSO = null;
            re = null;
            return i;
        }
    }
    

    Or you could use an array os pairs, and iterate through these pairs and pass any number of pairs...

    renameFiles(sFolder, [
        [sStringToFind1, sStringToReplace1],
        [sStringToFind2, sStringToReplace2],
        // ...,
        [sStringToFindN, sStringToReplaceN],
    ]);
    
    function renameFiles(sFolder, arrayOfPairs) {
        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;
    
                for (var j = 0; j < arrayOfPairs.length; j++) {
                    sName = sName.replace(arrayOfPairs[j][0], arrayOfPairs[j][1]);
                }
    
                if (oFile.Name != sName) {
                    i++;
                    oFile.Name = sName;
                }
            }
        }
        catch(e) {
            WScript.Echo("Could not access folder " + sFolder + " because\n" + e.description);
            return 0;
        } finally {
            oFSO = null;
            re = null;
            return i;
        }
    }