FANDOM


The following functions will take a path to a file and return various portions of it.

getFileNameEdit

function getFilename(path) {
  var pos = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'));
  return path.substr(pos + 1);
}

Results

  • getFilename("/Applications/Utilities/Activity Monitor.app") == "Activity Monitor.app"
  • getFilename("C:\\Program Files\\Pixoria\\Konfabulator\\README.rtf") == "README.rtf"
  • getFilename("C:\\WINDOWS\\") == ""
    (Not recommended - use filesystem.isDirectory() before calling getFilename)


getExtensionEdit

function getExtension(path) {
  var pos = path.lastIndexOf('.');
  return (pos < 0) ? "" : path.substr(pos + 1);
}

Results

  • getExtension("/Applications/Utilities/Activity Monitor.app") == "app"
  • getExtension("C:\\Program Files\\Pixoria\\Konfabulator\\README.rtf") == "rtf"
  • getExtension("~/Desktop/foo") == ""
  • getExtension("C:\\WINDOWS\\") == ""
    (Not recommended - use filesystem.isDirectory() before calling getExtension)


getFilenameWithoutExtensionEdit

function getFilenameWithoutExtension(path) {
  var pos1 = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'));
  var pos2 = path.lastIndexOf('.');
  return (pos2 < pos1) ?
    path.substr(pos1 + 1) : path.substr(pos1 + 1, pos2 - (pos1 + 1));
}

Results

  • getFilenameWithoutExtension("/Applications/Utilities/Activity Monitor.app") == "Activity Monitor"
  • getFilenameWithoutExtension("C:\\Program Files\\Pixoria\\Konfabulator\\README.rtf") == "README"
  • getFilenameWithoutExtension("~/Desktop/foo") == "foo"
  • getFilenameWithoutExtension("C:\\WINDOWS\\") == ""
    (Not recommended - use filesystem.isDirectory() before calling getFilenameWithoutExtension)


getFolderEdit

function getFolder(path) {
  var pos = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'));
  return path.substr(0, pos + 1);
}

Results

  • getFolder("/Applications/Utilities/Activity Monitor.app") == "/Applications/Utilities/"
  • getFolder("C:\\Program Files\\Pixoria\\Konfabulator\\README.rtf") == "C:\\Program Files\\Pixoria\\Konfabulator\\"
  • getFolder("~/Desktop/foo") == "~/Desktop/"
  • getFolder("C:\\WINDOWS\\") == ""
    (Not recommended - use filesystem.isDirectory() before calling getFolder)


See AlsoEdit


External linksEdit

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.