GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Advanced Slugify / Web-Safe rename

jscript
scripting

#1

I don't really like the the older "Make Web-safe" rename script, because it removes all diacritic signs (like "ĘÓĄŚŁŻŹŃęóąśłżźćń") instead of replacing them with web safe characters. I've created a JScript which first replaces diacritics with their equivalents and then it finally does whitelist replacing, in case anything was missed. It also uses dashes instead of underscores. Of course you should add replacements from your language (this one does full polish replacement)

Any remarks appreciated.

Download

Script code

Use the download above if you just want to use the script. The code is reproduced below to help people browsing for scripting techniques on the forum.

This script is also on GitHub: Directory Opus JScript slugify/web-safe rename

function Rename::GetNewName2 ( strFileName, strFilePath, _fIsFolder, strOldName, strNewName ){
  r = strFileName;
  r = r.toLowerCase();

  r = r.replace(new RegExp("[àáâãäåą]", 'g'),"a");
  r = r.replace(new RegExp("[æ]", 'g'),"ae");
  r = r.replace(new RegExp("[çčçć]", 'g'),"c");
  r = r.replace(new RegExp("[èéěêëę]", 'g'),"e");
  r = r.replace(new RegExp("[ìíîï]", 'g'),"i");
  r = r.replace(new RegExp("[ñń]", 'g'),"n");
  r = r.replace(new RegExp("[òóôõöó]", 'g'),"o");
  r = r.replace(new RegExp("[œ]", 'g'),"oe");
  r = r.replace(new RegExp("[ùūúûü]", 'g'),"u");
  r = r.replace(new RegExp("[ł]", 'g'),"l");
  r = r.replace(new RegExp("[ð]", 'g'),"d");
  r = r.replace(new RegExp("[ś]", 'g'),"s");
  r = r.replace(new RegExp("[ýÿ]", 'g'),"y");
  r = r.replace(new RegExp("[żź]", 'g'),"z");

  r = r.replace(new RegExp("[É]", 'g'),"E");
  r = r.replace(new RegExp("[Ö]", 'g'),"O");
  r = r.replace(new RegExp("[Á]", 'g'),"A");


  //removing all characters except:
  r = r.replace(/[^-_a-zA-Z0-9.,\s]+/ig, '');
  // replacing all spaces with dashes
  r = r.replace(/\s/gi, "-");

  return r;
}

I've used these two sources:


Web rename script? (percent url ascii encoding)
#4

This has become an issue for now. Please be so kind and check the adjustments that I did for German language details. Does that make sense? Is that correct?

function Rename::GetNewName2 ( strFileName, strFilePath, _fIsFolder, strOldName, strNewName ){
  r = strFileName;
  r = r.toLowerCase();
 
  r = r.replace(new RegExp("[àáâãåą]", 'g'),"a");
  r = r.replace(new RegExp("[æä]", 'g'),"ae");
  r = r.replace(new RegExp("[çčçć]", 'g'),"c");
  r = r.replace(new RegExp("[èéěêëę]", 'g'),"e");
  r = r.replace(new RegExp("[ìíîï]", 'g'),"i");
  r = r.replace(new RegExp("[ñń]", 'g'),"n");
  r = r.replace(new RegExp("[òóôõó]", 'g'),"o");
  r = r.replace(new RegExp("[Ϛ]", 'g'),"oe");
  r = r.replace(new RegExp("[ùūúû]", 'g'),"u");
  r = r.replace(new RegExp("[ü]", 'g'),"ue");
  r = r.replace(new RegExp("[ł]", 'g'),"l");
  r = r.replace(new RegExp("[ð]", 'g'),"d");
  r = r.replace(new RegExp("[ś]", 'g'),"s");
  r = r.replace(new RegExp("[ß]", 'g'),"ss");
  r = r.replace(new RegExp("[ýÿ]", 'g'),"y");
  r = r.replace(new RegExp("[żź]", 'g'),"z");
 
  r = r.replace(new RegExp("[É]", 'g'),"E");
  r = r.replace(new RegExp("[Ö]", 'g'),"O");
  r = r.replace(new RegExp("[Á]", 'g'),"A");
  r = r.replace(new RegExp("[Ä]", 'g'),"Ae");
  r = r.replace(new RegExp("[Ü]", 'g'),"Ue");
  r = r.replace(new RegExp("[Ö]", 'g'),"Oe");
 
 
  //removing all characters except:
  r = r.replace(/[^-_a-zA-Z0-9.,\s]+/ig, '');
  // replacing all spaces with dashes
  r = r.replace(/\s/gi, "-");
 
  return r;
}