JavaScript 在文件夹中批量重命名

JavaScript Batch-Renaming in a Folder

我有一对字符串需要在给定文件夹的文件名中替换,例如BUL 到 Bg-bg,ENG 到 En-us 等。有 12 个这样的对。我找到了一个脚本来处理一对替换,但每当我尝试添加更多参数时,它都会以某种方式只循环一个并只重命名一个匹配的字符串。无法想象我们有一个更简单的代码可以一次接受所有替换字符串对,而无需为每一对创建单独的循环。任何想法将不胜感激。

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

您可以使用 replace() 并在同一循环内进行所有替换:

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

或者您可以使用数组 os 对,并遍历这些对并传递任意数量的对...

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