GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Remove All After

user-interface
jscript
scripting
regex

#1

This rename preset requires DOpus 12 (or later).

Overview:

Removes everything after the matched text (not including the fileextension).
Very useful to quickly remove random "garbage" from filenames.

Adds an extra field to the rename dialog where you can type a regular expression:

Anything after the part of the file name which matches the regular expression will be removed.

Example:

If you have the filename This is a test.txt and you enter the regex \bis into the Remove All After field then the resulting filename will be This is.txt.

Download:

Script code:

If you just want to use the script, use the download above. The script code is reproduced here to help people browsing the forum for scripting techniques.

// Remove All After
// Removes everything following the matched regex
/* exported OnGetNewName,OnGetCustomFields */

// adds custom fields to the Rename dialog
function OnGetCustomFields(getCustomFieldData){
  getCustomFieldData.fields.removeallafter='';
  getCustomFieldData.field_labels('removeallafter')='Remove all after'; //jshint ignore:line
  getCustomFieldData.field_tips('removeallafter')='Enter a regular expression to match'; //jshint ignore:line
}

function OnGetNewName(getNewNameData){
  var removeallafter=getNewNameData.custom.removeallafter;
  var name=getNewNameData.newname_stem;
  var re;
  var matches;
  if (name!=''){
    if (removeallafter){
      re=new RegExp(removeallafter,'i');
      matches=re.exec(name);
      if (matches){
        name=name.substr(0,matches.lastIndex);
      }
    }
  }
  return name+getNewNameData.newname_ext;
}