Dart API Referencedart:ioDirectory

Directory abstract class

Directory objects are used for working with directories.

abstract class Directory extends FileSystemEntity {
 /**
  * Creates a directory object. The path is either an absolute path,
  * or it is a relative path which is interpreted relative to the directory
  * in which the Dart VM was started.
  */
 factory Directory(String path) => new _Directory(path);

 /**
  * Creates a directory object from a Path object. The path is either
  * an absolute path, or it is a relative path which is interpreted
  * relative to the directory in which the Dart VM was started.
  */
 factory Directory.fromPath(Path path) => new _Directory.fromPath(path);

 /**
  * Creates a directory object pointing to the current working
  * directory.
  */
 static Directory get current => _Directory.current;

 /**
  * Sets the current working directory of the Dart process including
  * all running isolates. The new value set can be either a [Directory]
  * or a [String].
  *
  * The new value is passed to the OS's system call unchanged, so a
  * relative path passed as the new working directory will be
  * resolved by the OS.
  *
  * Note that setting the current working directory is a synchronous
  * operation and that it changes the the working directory of *all*
  * isolates.
  *
  * Use this with care - especially when working with asynchronous
  * operations and multiple isolates. Changing the working directory,
  * while asynchronous operations are pending or when other isolates
  * are working with the file system, can lead to unexpected results.
  */
 static void set current(path) {
   _Directory.current = path;
 }

 /**
  * Check whether a directory with this name already exists. Returns
  * a [:Future<bool>:] that completes with the result.
  */
 Future<bool> exists();

 /**
  * Synchronously check whether a directory with this name already exists.
  */
 bool existsSync();

 /**
  * Creates the directory with this name.
  *
  * If [recursive] is false, only the last directory in the path is
  * created. If [recursive] is true, all non-existing path components
  * are created. If the directory already exists nothing is done.
  *
  * Returns a [:Future<Directory>:] that completes with this
  * directory once it has been created. If the directory cannot be
  * created the future completes with an exception.
  */
 Future<Directory> create({recursive: false});

 /**
  * Synchronously creates the directory with this name.
  *
  * If [recursive] is false, only the last directory in the path is
  * created. If [recursive] is true, all non-existing path components
  * are created. If the directory already exists nothing is done.
  *
  * If the directory cannot be created an exception is thrown.
  */
 void createSync({recursive: false});

 /**
  * Creates a temporary directory with a name based on the current
  * path.  The path is used as a template, and additional
  * characters are appended to it to make a unique temporary
  * directory name.  If the path is the empty string, a default
  * system temp directory and name are used for the template.
  *
  * Returns a [:Future<Directory>:] that completes with the newly
  * created temporary directory.
  */
 Future<Directory> createTemp();

 /**
  * Synchronously creates a temporary directory with a name based on the
  * current path. The path is used as a template, and additional
  * characters are appended to it to make a unique temporary directory name.
  * If the path is the empty string, a default system temp directory and name
  * are used for the template. Returns the newly created temporary directory.
  */
 Directory createTempSync();

 /**
  * Deletes this directory.
  *
  * If [recursive] is false, the directory must be empty.  Only directories
  * and links to directories will be deleted.
  *
  * If [recursive] is true, this directory and all sub-directories
  * and files in the directories are deleted. Links are not followed
  * when deleting recursively. Only the link is deleted, not its target.
  *
  * If [recursive] is true, the target is deleted even if it is a file, or
  * a link to a file, not only if it is a directory.  This behavior allows
  * [delete] to be used to unconditionally delete any file system object.
  *
  * Returns a [:Future<Directory>:] that completes with this
  * directory when the deletion is done. If the directory cannot be
  * deleted, the future completes with an exception.
  */
 Future<Directory> delete({recursive: false});

 /**
  * Synchronously deletes this directory.
  *
  * If [recursive] is false, the directory must be empty.
  *
  * If [recursive] is true, this directory and all sub-directories
  * and files in the directories are deleted. Links are not followed
  * when deleting recursively. Only the link is deleted, not its target.
  *
  * If [recursive] is true, the target is deleted even if it is a file, or
  * a link to a file, not only if it is a directory.  This behavior allows
  * [delete] to be used to unconditionally delete any file system object.
  *
  * Throws an exception if the directory cannot be deleted.
  */
 void deleteSync({recursive: false});

 /**
  * Renames this directory. Returns a [:Future<Directory>:] that completes
  * with a [Directory] instance for the renamed directory.
  *
  * If newPath identifies an existing directory, that directory is
  * replaced. If newPath identifies an existing file, the operation
  * fails and the future completes with an exception.
  */
 Future<Directory> rename(String newPath);

 /**
  * Synchronously renames this directory. Returns a [Directory]
  * instance for the renamed directory.
  *
  * If newPath identifies an existing directory, that directory is
  * replaced. If newPath identifies an existing file the operation
  * fails and an exception is thrown.
  */
 Directory renameSync(String newPath);

 /**
  * Lists the sub-directories and files of this [Directory].
  * Optionally recurses into sub-directories.
  *
  * If [followLinks] is false, then any symbolic links found
  * are reported as [Link] objects, rather than as directories or files,
  * and are not recursed into.
  *
  * If [followLinks] is true, then working links are reported as
  * directories or files, depending on
  * their type, and links to directories are recursed into.
  * Broken links are reported as [Link] objects.
  * If a symbolic link makes a loop in the file system, then a recursive
  * listing will not follow a link twice in the
  * same recursive descent, but will report it as a [Link]
  * the second time it is seen.
  *
  * The result is a stream of [FileSystemEntity] objects
  * for the directories, files, and links.
  */
 Stream<FileSystemEntity> list({bool recursive: false,
                                bool followLinks: true});

 /**
  * Lists the sub-directories and files of this [Directory].
  * Optionally recurses into sub-directories.
  *
  * If [followLinks] is false, then any symbolic links found
  * are reported as [Link] objects, rather than as directories or files,
  * and are not recursed into.
  *
  * If [followLinks] is true, then working links are reported as
  * directories or files, depending on
  * their type, and links to directories are recursed into.
  * Broken links are reported as [Link] objects.
  * If a link makes a loop in the file system, then a recursive
  * listing will not follow a link twice in the
  * same recursive descent, but will report it as a [Link]
  * the second time it is seen.
  *
  * Returns a [List] containing [FileSystemEntity] objects for the
  * directories, files, and links.
  */
 List<FileSystemEntity> listSync({bool recursive: false,
                                  bool followLinks: true});

 /**
  * Returns a human readable string for this Directory instance.
  */
 String toString();

 /**
  * Gets the path of this directory.
  */
 final String path;
}

Extends

FileSystemEntity > Directory

Static Properties

Directory get current #

Creates a directory object pointing to the current working directory.

static Directory get current => _Directory.current;

void set current(path) #

Sets the current working directory of the Dart process including all running isolates. The new value set can be either a Directory or a String.

The new value is passed to the OS's system call unchanged, so a relative path passed as the new working directory will be resolved by the OS.

Note that setting the current working directory is a synchronous operation and that it changes the the working directory of all isolates.

Use this with care - especially when working with asynchronous operations and multiple isolates. Changing the working directory, while asynchronous operations are pending or when other isolates are working with the file system, can lead to unexpected results.

static void set current(path) {
 _Directory.current = path;
}

Constructors

factory Directory(String path) #

Creates a directory object. The path is either an absolute path, or it is a relative path which is interpreted relative to the directory in which the Dart VM was started.

factory Directory(String path) => new _Directory(path);

factory Directory.fromPath(Path path) #

Creates a directory object from a Path object. The path is either an absolute path, or it is a relative path which is interpreted relative to the directory in which the Dart VM was started.

factory Directory.fromPath(Path path) => new _Directory.fromPath(path);

Properties

final String path #

Gets the path of this directory.

final String path

Methods

abstract Future<Directory> create({recursive: false}) #

Creates the directory with this name.

If recursive is false, only the last directory in the path is created. If recursive is true, all non-existing path components are created. If the directory already exists nothing is done.

Returns a Future<Directory> that completes with this directory once it has been created. If the directory cannot be created the future completes with an exception.

abstract void createSync({recursive: false}) #

Synchronously creates the directory with this name.

If recursive is false, only the last directory in the path is created. If recursive is true, all non-existing path components are created. If the directory already exists nothing is done.

If the directory cannot be created an exception is thrown.

abstract Future<Directory> createTemp() #

Creates a temporary directory with a name based on the current path. The path is used as a template, and additional characters are appended to it to make a unique temporary directory name. If the path is the empty string, a default system temp directory and name are used for the template.

Returns a Future<Directory> that completes with the newly created temporary directory.

abstract Directory createTempSync() #

Synchronously creates a temporary directory with a name based on the current path. The path is used as a template, and additional characters are appended to it to make a unique temporary directory name. If the path is the empty string, a default system temp directory and name are used for the template. Returns the newly created temporary directory.

abstract Future<Directory> delete({recursive: false}) #

Deletes this directory.

If recursive is false, the directory must be empty. Only directories and links to directories will be deleted.

If recursive is true, this directory and all sub-directories and files in the directories are deleted. Links are not followed when deleting recursively. Only the link is deleted, not its target.

If recursive is true, the target is deleted even if it is a file, or a link to a file, not only if it is a directory. This behavior allows delete to be used to unconditionally delete any file system object.

Returns a Future<Directory> that completes with this directory when the deletion is done. If the directory cannot be deleted, the future completes with an exception.

abstract void deleteSync({recursive: false}) #

Synchronously deletes this directory.

If recursive is false, the directory must be empty.

If recursive is true, this directory and all sub-directories and files in the directories are deleted. Links are not followed when deleting recursively. Only the link is deleted, not its target.

If recursive is true, the target is deleted even if it is a file, or a link to a file, not only if it is a directory. This behavior allows delete to be used to unconditionally delete any file system object.

Throws an exception if the directory cannot be deleted.

abstract Future<bool> exists() #

Check whether a directory with this name already exists. Returns a Future<bool> that completes with the result.

abstract bool existsSync() #

Synchronously check whether a directory with this name already exists.

abstract Stream<FileSystemEntity> list({bool recursive: false, bool followLinks: true}) #

Lists the sub-directories and files of this Directory. Optionally recurses into sub-directories.

If followLinks is false, then any symbolic links found are reported as Link objects, rather than as directories or files, and are not recursed into.

If followLinks is true, then working links are reported as directories or files, depending on their type, and links to directories are recursed into. Broken links are reported as Link objects. If a symbolic link makes a loop in the file system, then a recursive listing will not follow a link twice in the same recursive descent, but will report it as a Link the second time it is seen.

The result is a stream of FileSystemEntity objects for the directories, files, and links.

abstract List<FileSystemEntity> listSync({bool recursive: false, bool followLinks: true}) #

Lists the sub-directories and files of this Directory. Optionally recurses into sub-directories.

If followLinks is false, then any symbolic links found are reported as Link objects, rather than as directories or files, and are not recursed into.

If followLinks is true, then working links are reported as directories or files, depending on their type, and links to directories are recursed into. Broken links are reported as Link objects. If a link makes a loop in the file system, then a recursive listing will not follow a link twice in the same recursive descent, but will report it as a Link the second time it is seen.

Returns a List containing FileSystemEntity objects for the directories, files, and links.

abstract Future<Directory> rename(String newPath) #

Renames this directory. Returns a Future<Directory> that completes with a Directory instance for the renamed directory.

If newPath identifies an existing directory, that directory is replaced. If newPath identifies an existing file, the operation fails and the future completes with an exception.

abstract Directory renameSync(String newPath) #

Synchronously renames this directory. Returns a Directory instance for the renamed directory.

If newPath identifies an existing directory, that directory is replaced. If newPath identifies an existing file the operation fails and an exception is thrown.

abstract String toString() #

Returns a human readable string for this Directory instance.