FileSystemDirectoryEntry.getDirectory()

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

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

FileSystemDirectoryEntry.getDirectory([path][, options][, successCallback][, errorCallback]);

Parameters

path Optional
A USVString 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 based on the FileSystemFlags dictionary, 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.
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 FileError object describing the error which occurred.

Return value

undefined.

Errors

If an error occurs and an errorCallback was specified, it gets called with a single parameter: a FileError object describing the error. The FileError.code specifies what type of error occurred, as follows:

FileError.NOT_FOUND_ERR
The create option was not specified (or was specified as false), and the directory doesn't exist.
FileError.PATH_EXISTS_ERR
The create and exclusive options were both true, indicating that the directory should be created but must not already exist, but the directory does in fact already exist.
FileError.SECURITY_ERR
The request to access the directory was denied for security reasons.
FileError.TYPE_MISMATCH_ERR
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.

FileSystemFlags

The options parameter is an object which is based on the FileSystemFlags dictionary; it provides flags which make it possible to adjust the behavior of the getDirectory() method.

create Optional
If this property is true, and the requested file or 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 file 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.

Values and results

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

Option values File/directory condition Result
create exclusive
false n/a[1] Path exists and matches the desired type (depending on whether the function called is getFile() or getDirectory() The successCallback is called with a FileSystemFileEntry if getFile() was called or a FileSystemDirectoryEntry if getDirectory() was called.
false n/a[1] Path exists but doesn't match the desired type The errorCallback is called with an appropriate error code (if the callback was provided).
true false Path exists The existing file or directory is removed and replaced with a new one, then the successCallback is called with a FileSystemFileEntry or a FileSystemDirectoryEntry, as appropriate.
true false Path doesn't exist The file or directory is created, then a FileSystemFileEntry or a FileSystemDirectoryEntry is passed to the successCallback, as appropriate.
true true Path exists The errorCallback is called with an appropriate error, such as FileError.PATH_EXISTS_ERR.
true true Path doesn't exist The file or directory is created, then a FileSystemFileEntry or a FileSystemDirectoryEntry is passed to the successCallback, as appropriate.

[1] When create is false, the value of exclusive is irrelevant and ignored.

Example

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", {}, function(dirEntry) {
    dirEntry.getFile(lang + "-dict.json", {}, function(fileEntry) {
      fileEntry.file(function(dictFile)) {
        let reader = new FileReader();

        reader.addEventListener("loadend", function() {
          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 Status Comment
File and Directory Entries API
The definition of 'getDirectory()' in that specification.
Draft Initial specification.

This API has no official W3C or WHATWG specification.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic support
ExperimentalDeprecatedNon-standard
Chrome Full support 13Edge No support NoFirefox Full support 50
Notes
Full support 50
Notes
Notes In Firefox, the errorCallback's input parameter is a DOMException rather than a FileError object.
IE No support NoOpera No support NoSafari No support NoWebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android Full support 50
Notes
Full support 50
Notes
Notes In Firefox, the errorCallback's input parameter is a DOMException rather than a FileError object.
Opera Android No support NoSafari iOS No support NoSamsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.
See implementation notes.
See implementation notes.

See also

Document Tags and Contributors

Contributors to this page: duncancumming, fscholz, chrisdavidmills, Sheppy
Last updated by: duncancumming,