Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1119341 of FileSystemDirectoryEntry

  • Revision slug: Web/API/FileSystemDirectoryEntry
  • Revision title: DirectoryEntry
  • Revision id: 1119341
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment Added bracket and semi-colon to code sampleWeb/API/DirectoryEntry Web/API/FileSystemDirectoryEntry

Revision Content

{{APIRef("File System API")}}{{Non-standard_header}}

The DirectoryEntry interface of the FileSystem API represents a directory in a file system. It includes methods for creating, reading, looking up, and recursively removing files in a directory.

About this document

This document was last updated on March 2, 2012, and follows the W3C Specifications (Working Draft) drafted on April 19, 2011.

This specification is abandoned, having failed to reach any substantial traction.

Basic concepts

You can create a new directory by calling DirectoryEntry.getDirectory(). If you want to create subdirectories, create each child directory in sequence. If you try creating a directory using a full path that includes parent directories that do not exist yet, an error is returned. So create the hierarchy by recursively adding a new path after creating the parent directory.

Example

In the following code snippet, we create a directory called "Documents."

// Taking care of the browser-specific prefixes.
window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem; 
window.directoryEntry = window.directoryEntry || window.webkitDirectoryEntry;

...

function onFs(fs){
  fs.root.getDirectory('Documents', {create:true}, function(directoryEntry){
    //directoryEntry.isFile === false
    //directoryEntry.isDirectory === true
    //directoryEntry.name === 'Documents'
    //directoryEntry.fullPath === '/Documents'
    
    }, onError);

  }

// Opening a file system with temporary storage
window.requestFileSystem(TEMPORARY, 1024*1024 /*1MB*/, onFS, onError);

Method overview

DirectoryReader createReader ();
void getFile (in DOMString path, in optional Flags options, in optional EntryCallback successCallback, in optional ErrorCallback errorCallback);
void getDirectory (in DOMString path, in optional Flags options, in optional EntryCallback successCallback, in optional ErrorCallback errorCallback);
void removeRecursively (in Void Callback successCallback, in optional ErrorCallback errorCallback);

Methods

createReader()

Creates a new DirectoryReader to read entries from this directory. 

DirectoryReader createReader ();
Returns
DirectoryReader

getFile()

Depending on how you've set the options parameter, this method either creates a file or looks up an existing file.

void getFile (
  in DOMString path, in optional Flags options, in optional EntryCallback successCallback, in optional ErrorCallback errorCallback
);
Parameter
path
Either an absolute path or a relative path from the directory to the file to be looked up or created. You cannot create a file whose immediate parent does not exist. Create the parent directory first.
options
An object literal describing the behavior of the method. If the file does not exist, it is created.
Object literal Condition Result
create: true
exclusive: true
Path already exists An error is thrown.
create: true
exclusive: false
Path doesn't exist, and no other error occurs A file is created. If a file already exists, no error is thrown.
create: false
(exclusive is ignored)
Path exists The file is returned.
create: false
(exclusive is ignored)
Path doesn't exist An error is thrown.
create: false
(exclusive is ignored)
Path exists, but is a directory An error is thrown.
successCallback
A callback that is called to return the file selected or created.
errorCallback
A callback that is called when errors occur.
Returns
void

getDirectory()

Creates or looks up a directory. The methods are similar to getFile() with DirectoryEntry being passed in the success callback.

void getDirectory (
  in DOMString path, in optional Flags options, in optional EntryCallback successCallback, in optional ErrorCallback errorCallback
);
Parameter
path
Either an absolute path or a relative path from the DirectoryEntry to the directory to be looked up or created. It is an error to attempt to create a file whose immediate parent does not yet exist.
options
An object literal describing the behavior of the method if the directory does not exist.
Object literal Condition Result
create: true
exclusive: true
Path already exists An error is thrown.
create: true
exclusive: false
Path doesn't exist, and no other error occurs A directory is created and return a corresponding DirectoryEntry. If a directory already exists, no error is thrown.
create: false
(exclusive is ignored)
Path exists The directory corresponding to the path is returned.
create: false
(exclusive is ignored)
Path doesn't exist An error is thrown.
create: false
(exclusive is ignored)
Path exists, but is a file An error is thrown.
successCallback
A callback that is called to return the directory selected or created.
errorCallback
A callback that is called when errors happen.
Returns

void

removeRecursively()

Deletes a directory and all of its contents. You cannot delete the root directory of a file system.

If you delete a directory containing a file that cannot be removed or if an error occurs while the deletion is in progress, some of the contents might not be deleted. Catch these cases with error callbacks and retry the deletion.

DOMString removeRecursively (
  in VoidCallback successCallback, optional ErrorCallback errorCallback
);
Parameter
successCallback
A callback that is called when the deletion succeeds.
errorCallback
A callback that is called when errors happen.
Returns
void

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 13{{ property_prefix("webkit") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{ CompatNo() }} 0.16{{ property_prefix("webkit") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}

See also

Specification: {{ spec("https://dev.w3.org/2009/dap/file-system/pub/FileSystem/", "File API: Directories and System Specification", "WD") }}

Reference: File System API

Introduction: Basic Concepts About the File System API

Revision Source

<p>{{APIRef("File System API")}}{{Non-standard_header}}</p>

<p>The <code>DirectoryEntry</code> interface of the <a href="/en/DOM/File_API/File_System_API" title="en/DOM/File_API/File_System_API">FileSystem API</a> represents a directory in a file system. It includes methods for creating, reading, looking up, and recursively removing files in a directory.</p>

<h2 id="About_this_document">About this document</h2>

<p>This document was last updated on March 2, 2012, and follows the <a class="external" href="https://www.w3.org/TR/file-system-api/">W3C Specifications (Working Draft)</a> drafted on April 19, 2011.</p>

<p>This specification is abandoned, having failed to reach any substantial traction.</p>

<h2 id="basic_concepts" name="basic_concepts">Basic concepts</h2>

<p>You can create a new directory by calling <code>DirectoryEntry.getDirectory()</code>. If you want to create subdirectories, create each child directory in sequence. If you try creating a directory using a full path that includes parent directories that do not exist yet, an error is returned. So create the hierarchy by recursively adding a new path after creating the parent directory.</p>

<h4 id="example" name="example">Example</h4>

<p>In the following code snippet, we create a directory called "Documents."</p>

<pre class="brush: js">
// Taking care of the browser-specific prefixes.
window.requestFileSystem&nbsp;&nbsp;=&nbsp;window.requestFileSystem&nbsp;||&nbsp;window.webkitRequestFileSystem;&nbsp;
window.directoryEntry = window.directoryEntry || window.webkitDirectoryEntry;

...

function onFs(fs){
  fs.root.getDirectory('Documents', {create:true}, function(directoryEntry){
    //directoryEntry.isFile === false
    //directoryEntry.isDirectory === true
    //directoryEntry.name === 'Documents'
    //directoryEntry.fullPath === '/Documents'
    
    }, onError);

  }

// Opening a file system with temporary storage
window.requestFileSystem(TEMPORARY, 1024*1024 /*1MB*/, onFS, onError);</pre>

<h2 id="Method_overview">Method overview</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <td><code>DirectoryReader <a href="#createReader" title="#createReader">createReader</a> ();</code></td>
  </tr>
  <tr>
   <td><code>void <a href="#getFile" title="#getFile">getFile</a> (in DOMString <em>path</em>, in optional Flags <em>options</em>, in optional <a class="external" href="https://www.w3.org/TR/file-system-api/#idl-def-EntryCallback">EntryCallback</a> <em>successCallback</em>, in optional <a class="external" href="https://www.w3.org/TR/file-system-api/#idl-def-ErrorCallback">ErrorCallback</a> <em>errorCallback</em>);</code></td>
  </tr>
  <tr>
   <td><code>void <a href="#getDirectory" title="#getDirectory">getDirectory</a> (in DOMString path, in optional Flags <em>options</em>, in optional EntryCallback <em>successCallback</em>, in optional ErrorCallback <em>errorCallback</em>);</code></td>
  </tr>
  <tr>
   <td><code>void <a href="#removeRecursively" title="#removeRecursively">removeRecursively</a> (in Void Callback <em>successCallback</em>, in optional ErrorCallback <em>errorCallback</em>); </code></td>
  </tr>
 </tbody>
</table>

<h2 id="Methods">Methods</h2>

<h3 id="createReader" name="createReader">createReader()</h3>

<p>Creates a new <a href="/en/docs/Web/API/DirectoryReader"><code>DirectoryReader</code></a> to read entries from this directory.&nbsp;</p>

<pre>
DirectoryReader createReader ();</pre>

<h5 id="Returns">Returns</h5>

<dl>
 <dt><code>DirectoryReader</code></dt>
</dl>

<h3 id="getFile" name="getFile">getFile()</h3>

<p>Depending on how you've set the <code>options</code> parameter, this method either creates a file or looks up an existing file.</p>

<pre>
void getFile (
  in DOMString <em>path</em>, in optional Flags <em>options</em>, in optional EntryCallback <em>successCallback</em>, in optional ErrorCallback <em>errorCallback</em>
);</pre>

<h5 id="Parameter">Parameter</h5>

<dl>
 <dt>path</dt>
 <dd>Either an absolute path or a relative path from the directory to the file to be looked up or created. You cannot create a file whose immediate parent does not exist. Create the parent directory first.</dd>
 <dt>options</dt>
 <dd>An object literal describing the behavior of the method. If the file does not exist, it is created.</dd>
</dl>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col" width="249">Object literal</th>
   <th scope="col" width="354">Condition</th>
   <th scope="col" width="872">Result</th>
  </tr>
  <tr>
   <td><code>create: true</code><br />
    <code>exclusive: true</code></td>
   <td>Path already exists</td>
   <td>An error is thrown.</td>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><code>create: true</code><br />
    <code>exclusive: false</code></td>
   <td>Path doesn't exist, and no other error occurs</td>
   <td>A file is created. If a file already exists, no error is thrown.</td>
  </tr>
  <tr>
   <td><code>create: false</code><br />
    (<code>exclusive</code> is ignored)</td>
   <td>Path exists</td>
   <td>The file is returned.</td>
  </tr>
  <tr>
   <td><code>create: false</code><br />
    (<code>exclusive</code> is ignored)</td>
   <td>Path doesn't exist</td>
   <td>An error is thrown.</td>
  </tr>
  <tr>
   <td><code>create: false</code><br />
    (<code>exclusive</code> is ignored)</td>
   <td>Path exists, but is a directory</td>
   <td>An error is thrown.</td>
  </tr>
 </tbody>
</table>

<dl>
 <dt>successCallback</dt>
 <dd>A callback that is called to return the file selected or created.</dd>
 <dt>errorCallback</dt>
 <dd>A callback that is called when errors occur.</dd>
</dl>

<h5 id="Returns_2">Returns</h5>

<dl>
 <dt><code>void</code></dt>
</dl>

<h3 id="getDirectory" name="getDirectory">getDirectory()</h3>

<p>Creates or looks up a directory. The methods are similar to <code>getFile()</code> with DirectoryEntry being passed in the success callback.</p>

<pre>
void getDirectory (
  in DOMString <em>path</em>, in optional Flags <em>options</em>, in optional EntryCallback <em>successCallback</em>, in optional ErrorCallback <em>errorCallback</em>
);</pre>

<h5 id="Parameter_2">Parameter</h5>

<dl>
 <dt>path</dt>
 <dd>Either an absolute path or a relative path from the <code>DirectoryEntry</code> to the directory to be looked up or created. It is an error to attempt to create a file whose immediate parent does not yet exist.</dd>
 <dt>options</dt>
 <dd>An object literal describing the behavior of the method if the directory does not exist.</dd>
</dl>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col" width="249">Object literal</th>
   <th scope="col" width="354">Condition</th>
   <th scope="col" width="872">Result</th>
  </tr>
  <tr>
   <td><code>create: true</code><br />
    <code>exclusive: true</code></td>
   <td>Path already exists</td>
   <td>An error is thrown.</td>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><code>create: true</code><br />
    <code>exclusive: false</code></td>
   <td>Path doesn't exist, and no other error occurs</td>
   <td>A directory is created and return a corresponding <code>DirectoryEntry</code>. If a directory already exists, no error is thrown.</td>
  </tr>
  <tr>
   <td><code>create: false</code><br />
    (<code>exclusive</code> is ignored)</td>
   <td>Path exists</td>
   <td>The directory corresponding to the path is returned.</td>
  </tr>
  <tr>
   <td><code>create: false</code><br />
    (<code>exclusive</code> is ignored)</td>
   <td>Path doesn't exist</td>
   <td>An error is thrown.</td>
  </tr>
  <tr>
   <td><code>create: false</code><br />
    (<code>exclusive</code> is ignored)</td>
   <td>Path exists, but is a file</td>
   <td>An error is thrown.</td>
  </tr>
 </tbody>
</table>

<dl>
 <dt>successCallback</dt>
 <dd>A callback that is called to return the directory selected or created.</dd>
 <dt>errorCallback</dt>
 <dd>A callback that is called when errors happen.</dd>
</dl>

<h5 id="Returns_3">Returns</h5>

<p><code>void</code></p>

<h3 id="removeRecursively" name="removeRecursively">removeRecursively()</h3>

<p>Deletes a directory and all of its contents. You cannot delete the root directory of a file system.</p>

<p>If you delete a directory containing a file that cannot be removed or if an error occurs while the deletion is in progress, some of the contents might not be deleted. Catch these cases with error callbacks and retry the deletion.</p>

<pre>
DOMString removeRecursively (
  <em>in </em>VoidCallback successCallback, optional ErrorCallback errorCallback
);</pre>

<h5 id="Parameter_3">Parameter</h5>

<dl>
 <dt>successCallback</dt>
 <dd>A callback that is called when the deletion succeeds.</dd>
 <dt>errorCallback</dt>
 <dd>A callback that is called when errors happen.</dd>
</dl>

<h5 id="Returns_4">Returns</h5>

<dl>
 <dt><code>void</code></dt>
</dl>

<h2 id="Browser_Compatibility" name="Browser_Compatibility">Browser compatibility</h2>

<p>{{ CompatibilityTable() }}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>13{{ property_prefix("webkit") }}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE&nbsp;Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{ CompatNo() }}</td>
   <td>0.16{{ property_prefix("webkit") }}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<p>Specification:&nbsp;{{ spec("https://dev.w3.org/2009/dap/file-system/pub/FileSystem/", "File API: Directories and System Specification", "WD") }}</p>

<p>Reference: <a href="/en/DOM/File_API/File_System_API" title="en/DOM/File_API/File_System_API">File System API</a></p>

<p>Introduction:&nbsp;<a href="/en/DOM/File_APIs/Filesystem/Basic_Concepts_About_the_Filesystem_API" title="en/DOM/File_APIs/Filesystem/Basic_Concepts_About_the_Filesystem_API">Basic Concepts About the File System API</a></p>
Revert to this revision