tags:

views:

1168

answers:

5

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 returns me

C:\Program Files\nant\bin

I would prefer a built in function that does the job, instead of having manually split the string and exclude the last one.

Edit: I am running on Windows

+1  A: 

The core Javascript language doesn't provide file/io functions. However if you're working in a Windows OS you can use the FileSystemObject (ActiveX/COM).

Note: Don't use this in the client script-side script of a web application though, it's best in other areas such as in Windows script host, or the server side of a web app where you have more control over the platform.

This page provides a good tutorial on how to do this.

Here's a rough example to do what you want:

   var fso, targetFilePath,fileObj,folderObj;

   fso = new ActiveXObject("Scripting.FileSystemObject");

   fileObj = fso.GetFile(targetFilePath);

   folderObj=fileObj.ParentFolder;

   alert(folderObj.Path);
Ash
Um... I'm pretty sure the questioner just wanted a simple string manipulation function, not access to the file system!
brianpeiris
Using activeX is not a good idea. This will only work on IE. It will not work on Firefox or any other browser.
Nadia Alramli
@brianpeiris, Please read the question carefully. They say: "instead of having manually split the string and exclude the last one"
Ash
He also said he wanted to use functionality "built-in" to JS, and proprietary ActiveX extensions are anything but.
Matthew Flaschen
@Nadia, Thats why I clearly say "If you're on Windows". Also, I'm well aware how stupid this would be for a normal web app. However we aare talking about file manipulation so I assume the questions relates to server side code or scripting outside the browser.
Ash
@Matthew, you don't say! I've explained my rationale in the answer. In a controlled environment where you know you have access to it, FileSystemObject is a valuable tool.
Ash
+1  A: 

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);
Phaedrus
Your solution will only work on windows. Linux uses '/' instead. It's better to use an OS independent solution.
Nadia Alramli
@Nadia the question is about Windows and path separators could be the least of his concerns with portability, we haven't seen the code or know what he's using it for
Chad Grant
+2  A: 

There's no perfect solution, because this functionality isn't built-in, and there's no way to get the system file-separator. You can try:

path = path.substring(0, Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"))); alert(path);

Matthew Flaschen
+1  A: 
function getFileDirectory(filePath) {
  if (filePath.indexOf("/") == -1) { // windows
    return filePath.substring(0, filePath.lastIndexOf('\\'));
  } 
  else { // unix
    return filePath.substring(0, filePath.lastIndexOf('/'));
  }
}
console.assert(getFileDirectory('C:\\Program Files\\nant\\bin\\nant.exe') === 'C:\\Program Files\\nant\\bin');
console.assert(getFileDirectory('/usr/bin/nant') === '/usr/bin');
brianpeiris
A: 

write some code?

search backward for \ or //, create a new substring

Mark Essel