Getting portions of a file path

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

getFileName
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)

getExtension
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)

getFilenameWithoutExtension
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)

getFolder
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)