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 = 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 'package:node_io/node_io.dart';

void main() {
  // Creates dir/ and dir/subdir/.
  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 'package:node_io/node_io.dart';

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);
    });
}
Implemented types

Constructors

Directory(String path)

Properties

absolute → Directory
A FileSystemEntity whose path is the absolute path of path.
no setteroverride
basename String
Gets the part of this entity's path after the last separator.
no setterinherited
dirname String
Gets the part of this entity's path before the last separator.
no setterinherited
fileSystem → FileSystem
Returns the file system responsible for this entity.
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
isAbsolute bool
Whether this object's path is absolute.
no setterinherited
parent → Directory
The parent directory of this entity.
no setterinherited
path String
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
uri Uri
A Uri representing the file system entity's location.
no setterinherited

Methods

childDirectory(String basename) → Directory
Returns a reference to a Directory that exists as a child of this directory and has the specified basename.
childFile(String basename) → File
Returns a reference to a File that exists as a child of this directory and has the specified basename.
Returns a reference to a Link that exists as a child of this directory and has the specified basename.
create({bool recursive = false}) Future<Directory>
Creates the directory if it doesn't exist.
createSync({bool recursive = false}) → void
Synchronously creates the directory if it doesn't exist.
override
createTemp([String? prefix]) Future<Directory>
Creates a temporary directory in this directory.
createTempSync([String? prefix]) → Directory
Synchronously creates a temporary directory in this directory.
delete({bool recursive = false}) Future<FileSystemEntity>
Deletes this FileSystemEntity.
deleteSync({bool recursive = false}) → void
Synchronously deletes this FileSystemEntity.
override
exists() Future<bool>
Checks whether the file system entity with this path exists.
override
existsSync() bool
Synchronously checks whether the file system entity with this path exists.
override
list({bool recursive = false, bool followLinks = true}) Stream<FileSystemEntity>
Lists the sub-directories and files of this Directory.
listSync({bool recursive = false, bool followLinks = true}) List<FileSystemEntity>
Lists the sub-directories and files of this Directory. Optionally recurses into sub-directories.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
rename(String newPath) Future<Directory>
Renames this file system entity.
override
renameSync(String newPath) → Directory
Synchronously renames this file system entity.
override
Resolves the path of a file system object relative to the current working directory.
inherited
resolveSymbolicLinksSync() String
Resolves the path of a file system object relative to the current working directory.
inherited
stat() Future<FileStat>
Calls the operating system's stat() function on path.
inherited
statSync() FileStat
Synchronously calls the operating system's stat() function on path.
inherited
toString() String
A string representation of this object.
watch({int events = io.FileSystemEvent.all, bool recursive = false}) Stream<FileSystemEvent>
Start watching the FileSystemEntity for changes.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Properties

current ↔ Directory
Creates a directory object pointing to the current working directory.
getter/setter pair
systemTemp → Directory
Gets the system temp directory.
no setter