How to get folder path in javascript

I am trying to get the directory location of a file, and I'm not sure how to get it. I can't seem to find a module that allows me to do this.

So for example say I have this string:

/this/is/a/path/to/a/file.html

how can I get this:

/this/is/a/path/to/a

I know I can use something like this:

path.substr(0, path.lastIndexOf("/") - 1);

But I am not sure if that is as good of a method as something that might be built in to node.

I have also tried:

var info = url.parse(full_path);
console.log(info);

and the result doesn't return what I am looking for, that gets the full path including the filename.

So, is there something built into node that can do this and do it well?

The FileSystemDirectoryEntry interface's method getDirectory() returns a FileSystemDirectoryEntry object corresponding to a directory contained somewhere within the directory subtree rooted at the directory on which it's called.

Syntax

getDirectory()
getDirectory(path)
getDirectory(path, options)
getDirectory(path, options, successCallback)
getDirectory(path, options, successCallback, errorCallback)

Parameters

path Optional

A string representing an absolute path or a path relative to the directory on which the method is called, describing which directory entry to return. Absolute paths may not be able to be used, for security reasons.

options Optional

An object which allows you to specify whether or not to create the entry if it's missing and if it's an error if the file already exists. These options are currently not useful in Web contexts. See the options parameter section for more details.

successCallback Optional

A method to be called once the FileSystemDirectoryEntry has been created. The method receives a single parameter: the FileSystemDirectoryEntry object representing the directory in question.

errorCallback Optional

A method to be called if an error occurs. Receives as its sole input parameter a DomException object describing the error which occurred.

options parameter

The options parameter object accepts the following parameters:

create Optional

If this property is true, and the requested directory doesn't exist, the user agent should create it. The default is false. The parent directory must already exist.

exclusive Optional

If true, and the create option is also true, the directory must not exist prior to issuing the call. Instead, it must be possible for it to be created newly at call time. The default is false. This parameter is ignored if create is false.

The table below describes the result of each possible combination of these flags depending on whether or not the target directory path already exists.

Return value

Exceptions

NotFoundError DOMException

Thrown if the create option was not specified (or was specified as false), and the directory doesn't exist.

SecurityError DOMException

Thrown if the request to access the directory was denied for security reasons.

TypeMismatchError DOMException

Thrown if the path specified is not a directory; it's probably a file, but might be an unsupported file descriptor such as a pipe; this depends on the user agent to some extent.

Examples

In this example, a function is presented whose job it is to locate within a user's app data directory a JSON file containing a user dictionary for a specified language, then load that dictionary.

let dictionary = null;

function loadDictionaryForLanguage(appDataDirEntry, lang) {
  dictionary = null;

  appDataDirEntry.getDirectory("Dictionaries", {}, (dirEntry) => {
    dirEntry.getFile(`${lang}-dict.json`, {}, (fileEntry) => {
      fileEntry.file((dictFile) => {
        let reader = new FileReader();

        reader.addEventListener("loadend", () => {
          dictionary = JSON.parse(reader.result);
        });

        reader.readAsText(dictFile);
      });
    });
  });
}

The loadDictionaryForLanguage() function starts by using getDirectory() to obtain the FileSystemDirectoryEntry object representing a subfolder named "Dictionaries" located inside the specified app data directory. The success callback for this takes the resulting directory entry object and calls getFile() to get a FileSystemFileEntry object representing the dictionary file; the success callback for this, in turn, creates a new FileReader and uses it to load the contents of the file. When that is loaded successfully (as indicated by the loadend event being fired), the loaded text is passed into JSON.parse() to be reconstituted into a JavaScript object.

Specifications

Specification
File and Directory Entries API
# dom-filesystemdirectoryentry-getdirectory

Browser compatibility

BCD tables only load in the browser

See also

How do I know the path of a folder?

To view the full path of a folder:.
Click the Start button and then click Computer, click to open the location of the desired folder, and then right-click to the right of the path in the address bar..
On the menu, there are three options to choose from that will allow you to either copy or view the entire folder path:.

How do I give a file path in JavaScript?

“how to write file paths in javascript” Code Answer.
/ = Root directory..
. = This location..
.. = Up a directory..
./ = Current directory..
../ = Parent of current directory..
../../ = Two directories backwards..

How do I get the current directory in JavaScript?

js, there are two built-in ways to get the current directory. You can either use the __dirname variable or the process. cwd() method to get the current folder.

How do I get the path of a directory in HTML?

One can find the path of the file by using two attributes called src or href. Those attributes help us to attach an external file or source with our HTML document..