JavaScript在文件夹中批量重命名

时间:2018-03-13 17:42:04

标签: rename batch-processing wsh jscript batch-rename

我有一对需要在给定文件夹的文件名中替换的字符串,例如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;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用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;
    }
}