How to get the directory of a file?
For example, I pass in a string
C:\\Program Files\\nant\\bin\\nant.exe
I want a function that r
filepath.split("/").slice(0,-1).join("/"); // get dir of filepath
such that
"/path/to/test.js".split("/").slice(0,-1).join("/") == "/path/to"
I don't know if there is any built in functionality for this, but it's pretty straight forward to get the path.
path = path.substring(0,path.lastIndexOf("\\")+1);
Use:
var dirname = filename.match(/(.*)[\/\\]/)[1]||'';
*The answers that are based on lastIndexOf('/') or lastIndexOf('\') are error prone, because path can be "c:\aa/bb\cc/dd".
(Matthew Flaschen did took this into account, so my answer is a regex alternative)