ZetCode

Dart Directory

last modified January 28, 2024

In this article we show how to work with directories in Dart language. In our examples we create directories, delete them, list directory contents and get their stats.

A directory, also called a folder, is a location for storing files on your computer. A directory can also stores other directories or shortcuts.

In Dart, we use the Directory class to work with directories. It is a reference to a directory on the file system. The class is located in the dart:io package.

Dart create directory

We can use create and createSync member functions to create a directory in Dart.

main.dart
import 'dart:io';

void main() async {
  final path = 'doc/crypto';

  final dir = Directory(path);
  final ndir = await dir.create(recursive: true);
  print(ndir.path);
}

The program creates a new directory.

import 'dart:io';

We import the dart:io package.

final path = 'doc/crypto';

This is the path to the new directory.

final dir = Directory(path);

A Directory object is created with the specified path.

final ndir = await dir.create(recursive: true);

The create method creates the directory if it doesn't exist. If the directory already exists nothing is done. The recursive option ensures that all directories along the path are created. The function returns a Future<Directory> that completes with this directory once it has been created.

$ dart main.dart
doc/crypto

The createSync method creates the directory in synchronous mode.

main.dart
import 'dart:io';

void main() {
  final path = 'tmp/crypto';

  final dir = Directory(path);
  dir.createSync(recursive: true);
}

The program creates a new directory with createSync.

Dart delete directory

A directory can be deleted with delete and deleteSync functions.

main.dart
import 'dart:io';

void main() {
  final path = 'tmp';

  final dr = Directory(path);
  dr.deleteSync(recursive: true);
}

The program deleted the tmp directory. It recursively deletes also its crypto subdirectory.

Dart rename

We can rename directories with rename and renameSync.

main.dart
import 'dart:io';

void main() {
  final path = 'doc/crypto';
  final newPath = 'doc/cryptocurrency';

  final dr = Directory(path);
  final ndr = dr.renameSync(newPath);
  print(ndr.path);
}

The program renames the crypto directory to cryptocurrency with renameSync.

Dart list directory contents

Directory contents can be listed with list and listSync.

main.dart
import 'dart:io';

void main() {
  final path = '..';
  final dr = Directory(path);
  final files = dr.list();

  files.forEach((e) {
    print(e);
  });
}

The program lists the contents of the parent directory. The list function returns a stream of file system items. We loop over the stream with forEach.

$ dart main.dart
Directory: '..\control-flow'
Directory: '..\directory'
Directory: '..\ext-met'
Directory: '..\future'
Directory: '..\html'
Directory: '..\http-client'
Directory: '..\i2s'
Directory: '..\iterator'
File: '..\links.txt'
Directory: '..\pdf'
Directory: '..\process'
Directory: '..\runes'
Directory: '..\split-string'
Directory: '..\ssh-lib'
Directory: '..\stringbuffer'

Dart directory stats

The stat and statSync functions provide some basic filesystem stats about a directory.

main.dart
import 'dart:io';

void main() async {
  final path = 'doc/crypto';

  final dr = Directory(path);
  final stat = await dr.stat();

  print(stat);
}

The program prints the basic stats for doc/crypto directory.

$ dart main.dart
FileStat: type directory
          changed 2023-02-18 16:23:36.000
          modified 2023-02-18 16:23:36.000
          accessed 2023-02-19 17:34:53.000
          mode rwxrwxrwx
          size 0

Dart check if directory exists

We can check the directory existence with exists and existsSync.

main.dart
import 'dart:io';

void main() {
  final path = 'doc/crypto';
  final dr = Directory(path);

  checkIfExists(dr);
  deleteIfExists(dr);
  checkIfExists(dr);
}

void checkIfExists(Directory dr) {
  final present = dr.existsSync();
  print(present ? 'exists' : 'does not exist');
}

void deleteIfExists(Directory dr) {
  final present = dr.existsSync();
  if (present) {
    dr.deleteSync();
  }
}

In the program, we check if a directory exists, then delete the directory, and check for the existence again. We use the synchronous functions.

$ dart main.dart
exists
does not exist

Source

Dart Directory - language reference

In this article we have showed how to work with directories in Dart.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all Dart tutorials.