代码之家  ›  专栏  ›  技术社区  ›  Ilia Shifrin

在文件夹中批量重命名JavaScript

  •  0
  • Ilia Shifrin  · 技术社区  · 6 年前

    我在给定文件夹的文件名中有需要替换的字符串对,例如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 回复  |  直到 6 年前
        1
  •  0
  •   lmcarreiro    6 年前

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