Remove duplicate words from filenames (JScript array.indexOf error)

Here's an alternative version which uses the StringSet object provided by Opus, reducing the amount of code.

(In general, searching a set is usually more efficient than searching an array. In this case it won't make any real difference as filenames will only contain a handful of words. But it still makes things easier.)

Another difference is that the word matching is done case-insensitive (moo = Moo etc.).

If you want it to be case-sensitive, change DOpus.Create.StringSetI() to DOpus.Create.StringSet().

It also takes care of file extensions by using the built-in properties to split the stem and extension. (The original script would turn moo moo moo.txt into moo moo.txt as it saw the last word as moo.txt. The script below takes care of that.)

function OnGetNewName(getNewNameData)
{
    var splitby = " ";
    var joinwith = " ";
    var ArrOld = getNewNameData.newname_stem_m.split(splitby);
	var SetUnique = DOpus.Create.StringSetI();
    var ArrNew = [];

    for (var i = 0; i < ArrOld.length; i++)
    {
        if (SetUnique.insert(ArrOld[i]))
        {
            ArrNew.push(ArrOld[i]);
        }
    }

    return ArrNew.join(joinwith) + getNewNameData.newname_ext_m;
}
3 Likes