Get directory of a file name in Javascript

前端 未结 9 1513
囚心锁ツ
囚心锁ツ 2021-01-17 07:41

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

相关标签:
9条回答
  • 2021-01-17 08:10
    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');
    
    0 讨论(0)
  • 2021-01-17 08:15

    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);
    
    0 讨论(0)
  • 2021-01-17 08:17

    Sorry to bring this back up but was also looking for a solution without referencing the variable twice. I came up with the following:

    var filepath = 'C:\\Program Files\\nant\\bin\\nant.exe';
        // C:\Program Files\nant\bin\nant.exe
    var dirpath = filepath.split('\\').reverse().splice(1).reverse().join('\\');
        // C:\Program Files\nant\bin
    

    This is a bit of a walk through manipulating a string to array and back but it's clean enough I think.

    0 讨论(0)
  • 2021-01-17 08:18

    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);
    
    0 讨论(0)
  • 2021-01-17 08:21

    And this?

    If isn't a program in addressFile, return addressFile

    function(addressFile) {
        var pos = addressFile.lastIndexOf("/");
        pos = pos != -1 ? pos : addressFile.lastIndexOf("\\");
    
        if (pos > addressFile.lastIndexOf(".")) {
            return addressFile;
        }
    
        return addressFile.substring(
            0,
            pos+1
        );
    }
    
    
    console.assert(getFileDirectory('C:\\Program Files\\nant\\bin\\nant.exe') === 'C:\\Program Files\\nant\\bin\\');
    console.assert(getFileDirectory('/usr/bin/nant') === '/usr/bin/nant/');
    console.assert(getFileDirectory('/usr/thisfolderhaveadot.inhere') === '/usr/');
    
    0 讨论(0)
  • 2021-01-17 08:28

    If you use typescript, path module is quite handy.

    path.dirname("/home/workspace/filename.txt") // '/home/workspace/'
    
    0 讨论(0)
提交回复
热议问题