Mass renaming button, is there a way to compactify it?

Following snippet from a code is not elegant, i know. :slightly_smiling_face:

Is there a way to make the list more compact, like using brackets and pipe characters?

@NoDeselect
rename pattern " Cd " to " CD " findrep
@NoDeselect
rename pattern " Bbc " to " BBC " findrep
@NoDeselect
rename pattern " Cd" to " CD" findrep
@NoDeselect
rename pattern "Dvd" to "DVD" findrep
@NoDeselect
rename pattern " Dj " to " DJ " findrep
@NoDeselect
rename pattern "Mca" to "McA" findrep
@NoDeselect
rename pattern "Mcc" to "McC" findrep
@NoDeselect
rename pattern "Mcd" to "McD" findrep

You only need one @nodeselect.

You can't turn them all into a single pattern, since it wouldn't know which replacement to use. But you could use a rename preset which runs a script and does all of it at once, and then run that from a single command. That would be more efficient.

This preset/script would be a good starting point: Script to perform multiple Regular Expressions

Ok, i will remove all the extra @nodeselects then.

That's what i suspected. They all come in pairs.

I will have a look then. Thanks!

1 Like

If regular expression capture groups could be referred to in the new string markers with a Werb character that changes lower to upper case, this could be very simple to do.
I don't know really, perhaps something like /1\~[a-z] or something else ...

Well, scripting may be best, it's just a thought.

That could work for some of the renames. But the whole list is a mixed bag of things, so i would have to stick to some sort of list, or split it up into different rename buttons.

rename pattern " Cd " to " CD " findrep
rename pattern " Cd" to " CD" findrep

These could be combined into just rename pattern " Cd" to " CD" findrep for the second one will initiate anyways.

Another thing to note is that to convert to uppercase, a (VB/J)Script will be needed. So even if made more compact, it doesn't end up that much more compact (if at all).

With an if/else if VB/JScript, you could combine the Mc ones for one condition (Mc[acd]) and the rest for the other condition (separated by |), under one and the same button. If you would want to go that route.

Thanks, pro, i will try that rename pattern tip. As for the scripts, i would still have to learn the basics. :slightly_smiling_face:

Script Type: JScript

function OnClick(clickData) {
 var cmd = clickData.func.command,
     pattern1 = / Cd| Bbc |Dvd| Dj /g, // patterns to set to uppercase, with a g(lobal) modifier to change all occurances in the file name
     pattern2 = /(Mc)([acd])/g; // patterns to set acd to uppercase. Capture group $1 will be Mc, $2 will be [acd];
 cmd.deselect = false;
 for (var sel = new Enumerator(clickData.func.sourcetab.selected); !sel.atEnd(); sel.moveNext()) {
  var name = sel.item().name_stem, // name without extension
      newname = false; // variable to store the new, modified pattern
  if (name.match(pattern1)) {
   newname = name.replace(pattern1, function(match) {
    return match.toUpperCase();
   });
  } else if (name.match(pattern2)) { // remove "else" if you want to run pattern1 and pattern2 both on the same filename, if both can occur
   newname = name.replace(pattern2, function($0,$1,$2) {
    return $1+$2.toUpperCase();
   });
  }
  if (newname) { // just to make sure it won't run if there is no modified pattern, otherwise the new name may end up empty or undefined
   cmd.RunCommand("rename pattern \""+name+"\" to \""+newname+"\" findrep");
  }
 }
}

Should be even easier as a Rename preset/script, since then you don't need to worry about looping through the files, just what to name each file.

...and I thought I was the master of over-engineering :crazy_face:

1 Like

True, although in this case it's not that much shorter. I find that for beginners it's a bit more clear what is happening exactly and how to put it in the button so that it works. Also, the pattern can be put on or near the top (easier to reach and adjust).

As a rename script (Function: Standard Function):

@nodeselect
RENAME PATTERN="*" TO="*"
@script JScript
function OnGetNewName(getNewNameData) {
 var pattern1 = / Cd| Bbc |Dvd| Dj /g, // patterns to set to uppercase, with a g(lobal) modifier to change all occurances in the file name
     pattern2 = /(Mc)([acd])/g, // patterns to set acd to uppercase. Capture group $1 will be Mc, $2 will be [acd];
     file = getNewNameData.item,
     name = file.name_stem, // name without extension
     newname = false; // variable to store the new, modified pattern
 if (name.match(pattern1)) {
  newname = name.replace(pattern1, function(match) {
   return match.toUpperCase();
  });
 } else if (name.match(pattern2)) {
  newname = name.replace(pattern2, function($0,$1,$2) {
   return $1+$2.toUpperCase();
  });
 }
 if (newname) {
  return newname+file.ext;
 }
}

EDIT: a direct translation of the OP, making it much more compact (also without the comments), at the little cost of it being as efficient also:

@nodeselect
RENAME PATTERN="*" TO="*"
@script JScript
function OnGetNewName(getNewNameData) {
 var file = getNewNameData.item,
     newname = file.name_stem.replace(/ Cd| Bbc |Dvd| Dj /g, function(match) { return match.toUpperCase(); });
 newname = newname.replace(/(Mc)([acd])/g, function($0,$1,$2) { return $1+$2.toUpperCase(); });
 return newname+file.ext;
}
1 Like