W3cubDocs

/Dart 2

Directory class

A reference to a directory (or folder) on the file system.

A Directory instance is an object holding a path on which operations can be performed. The path to the directory can be absolute or relative. You can get the parent directory using the getter parent, a property inherited from FileSystemEntity.

In addition to being used as an instance to access the file system, Directory has a number of static properties, such as systemTemp, which gets the system's temporary directory, and the getter and setter current, which you can use to access or change the current directory.

Create a new Directory object with a pathname to access the specified directory on the file system from your program.

var myDir = new Directory('myDir');

Most methods in this class occur in synchronous and asynchronous pairs, for example, create and createSync. Unless you have a specific reason for using the synchronous version of a method, prefer the asynchronous version to avoid blocking your program.

Create a directory

The following code sample creates a directory using the create method. By setting the recursive parameter to true, you can create the named directory and all its necessary parent directories, if they do not already exist.

import 'dart:io';

void main() {
  // Creates dir/ and dir/subdir/.
  new Directory('dir/subdir').create(recursive: true)
    // The created directory is returned as a Future.
    .then((Directory directory) {
      print(directory.path);
  });
}

List a directory

Use the list or listSync methods to get the files and directories contained by a directory. Set recursive to true to recursively list all subdirectories. Set followLinks to true to follow symbolic links. The list method returns a Stream that provides FileSystemEntity objects. Use the listen callback function to process each object as it become available.

import 'dart:io';

void main() {
  // Get the system temp directory.
  var systemTempDir = Directory.systemTemp;

  // List directory contents, recursing into sub-directories,
  // but not following symbolic links.
  systemTempDir.list(recursive: true, followLinks: false)
    .listen((FileSystemEntity entity) {
      print(entity.path);
    });
}

The use of Futures

I/O operations can block a program for some period of time while it waits for the operation to complete. To avoid this, all methods involving I/O have an asynchronous variant which returns a Future. This future completes when the I/O operation finishes. While the I/O operation is in progress, the Dart program is not blocked, and can perform other operations.

For example, the exists method, which determines whether the directory exists, returns a boolean value using a Future. Use then to register a callback function, which is called when the value is ready.

import 'dart:io';

main() {
  final myDir = new Directory('dir');
  myDir.exists().then((isThere) {
    isThere ? print('exists') : print('non-existent');
  });
}

In addition to exists, the stat, rename, and other methods, return Futures.

Other resources

  • Dart by Example provides additional task-oriented code samples that show how to use various API from the Directory class and the related File class.

  • I/O for Command-Line Apps a section from A Tour of the Dart Libraries covers files and directories.

  • Write Command-Line Apps, a tutorial about writing command-line apps, includes information about files and directories.

Implemented types

Constructors

Directory(String path)
factory
Creates a Directory object. [...]
Directory.fromRawPath(Uint8List path)
factory
Directory.fromUri(Uri uri)
factory
Create a Directory object from a URI. [...]

Properties

absoluteDirectory
read-only, override
Returns a Directory instance whose path is the absolute path to this. [...]
pathString
read-only, override
Gets the path of this directory.
uriUri
read-only, override
Returns a Uri representing the directory's location. [...]
hashCodeint
read-only, inherited
The hash code for this object. [...]
isAbsolutebool
read-only, inherited
Returns a bool indicating whether this object's path is absolute. [...]
parentDirectory
read-only, inherited
The directory containing this.
runtimeTypeType
read-only, inherited
A representation of the runtime type of the object.

Methods

create({bool recursive: false }) → Future<Directory>
Creates the directory with this name. [...]
createSync({bool recursive: false }) → void
Synchronously creates the directory with this name. [...]
createTemp([String prefix ]) → Future<Directory>
Creates a temporary directory in this directory. Additional random characters are appended to prefix to produce a unique directory name. If prefix is missing or null, the empty string is used for prefix. [...]
createTempSync([String prefix ]) → Directory
Synchronously creates a temporary directory in this directory. Additional random characters are appended to prefix to produce a unique directory name. If prefix is missing or null, the empty string is used for prefix. [...]
list({bool recursive: false, bool followLinks: true }) → Stream<FileSystemEntity>
Lists the sub-directories and files of this Directory. Optionally recurses into sub-directories. [...]
listSync({bool recursive: false, bool followLinks: true }) → List<FileSystemEntity>
Lists the sub-directories and files of this Directory. Optionally recurses into sub-directories. [...]
rename(String newPath) → Future<Directory>
override
Renames this directory. Returns a Future<Directory> that completes with a Directory instance for the renamed directory. [...]
renameSync(String newPath) → Directory
override
Synchronously renames this directory. Returns a Directory instance for the renamed directory. [...]
Resolves the path of a file system object relative to the current working directory. [...]
resolveSymbolicLinksSync() → String
override
Resolves the path of a file system object relative to the current working directory. [...]
toString() → String
override
Returns a human readable string for this Directory instance.
delete({bool recursive: false }) → Future<FileSystemEntity>
inherited
Deletes this FileSystemEntity. [...]
deleteSync({bool recursive: false }) → void
inherited
Synchronously deletes this FileSystemEntity. [...]
exists() → Future<bool>
inherited
Checks whether the file system entity with this path exists. Returns a Future<bool> that completes with the result. [...]
existsSync() → bool
inherited
Synchronously checks whether the file system entity with this path exists. [...]
noSuchMethod(Invocation invocation) → dynamic
inherited
Invoked when a non-existent method or property is accessed. [...]
stat() → Future<FileStat>
inherited
Calls the operating system's stat() function on the path of this FileSystemEntity. [...]
statSync() → FileStat
inherited
Synchronously calls the operating system's stat() function on the path of this FileSystemEntity. [...]
watch({int events: FileSystemEvent.all, bool recursive: false }) → Stream<FileSystemEvent>
inherited
Start watching the FileSystemEntity for changes. [...]

Operators

operator ==(dynamic other) → bool
inherited
The equality operator. [...]

Static Properties

currentDirectory
read / write
Creates a directory object pointing to the current working directory.
systemTempDirectory
read-only
Gets the system temp directory. [...]

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dart.dev/stable/2.5.0/dart-io/Directory-class.html