60

I would like to list all the contents of a directory (on the file system) using Dart. How can I do this?

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Seth Ladd
  • 112,095
  • 66
  • 196
  • 279

9 Answers9

63

How to list the contents of a directory in Dart

final dir = Directory('path/to/directory');
final List<FileSystemEntity> entities = await dir.list().toList();

This creates a Directory from a path. Then it converts it to a list of FileSystemEntity, which can be a File, a Directory, or a Link. By default subdirectories are not listed recursively.

If you want to print that list, then add this line:

entities.forEach(print);

If you want to only get the files then you could do it like so:

final Iterable<File> files = entities.whereType<File>();
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • 7
    This should be the accepted answer. The ones above are either out of date in API and/or Dart style, or unnecessarily complex. – filiph Mar 04 '21 at 23:27
  • if you want to filter only files, then you use `element as file` or there is a better way ? ( where `element` is any element in the list that you called `files` ) – HII Aug 06 '21 at 00:05
  • 1
    @HaidarMehsen, I've updated my answer with one possible solution. I'm not sure if it is the best one but it seems to work. – Suragch Aug 06 '21 at 01:45
30

The API has changed and I have updated the async code for M4 release (0.5.16_r23799 ):

Future<List<FileSystemEntity>> dirContents(Directory dir) {
  var files = <FileSystemEntity>[];
  var completer = Completer<List<FileSystemEntity>>();
  var lister = dir.list(recursive: false);
  lister.listen ( 
      (file) => files.add(file),
      // should also register onError
      onDone:   () => completer.complete(files)
      );
  return completer.future;
}
Ryan Knell
  • 6,204
  • 2
  • 40
  • 32
Nico
  • 1,549
  • 2
  • 11
  • 9
  • 1
    You still need to get the file and directory names, which is surprisingly difficult. See http://stackoverflow.com/questions/17551088/how-can-i-get-the-name-of-a-file-in-dart. – Damien Aug 05 '15 at 18:25
  • 1
    [From Dart docs](https://api.dart.dev/stable/2.10.1/dart-io/FileSystemEntity-class.html): `FileSystemEntity` is superclass to `File`, `Directory` and `Link`. – Paul Kastel Oct 13 '20 at 08:57
  • 1
    How to filter with extension? – Pratik Butani Dec 29 '20 at 09:52
22

The list method returns a Stream where each emitted event is a directory entry:

Directory dir = Directory('.');
// execute an action on each entry
dir.list(recursive: false).forEach((f) {
  print(f);
});

As the name suggest, listSync method is the blocking version:

// create a list of entries
List<FileSystemEntity> entries = dir.listSync(recursive: false).toList();

What method to use depends on application context. A note directly from the docs:

Unless you have a specific reason for using the synchronous version of a method, prefer the asynchronous version to avoid blocking your program.

attdona
  • 17,196
  • 7
  • 49
  • 60
  • May be worth noting that with `Stream.forEach()` the result can be surprising if the callback function is marked `async`. See https://stackoverflow.com/a/59549511/3558355. – Chuck Batson Sep 09 '22 at 16:41
13

This answer is out of date. Please see the accepted answer.

There are two ways to list the contents of a directory using the Dart VM and the dart:io library.

(note: the following only works in the Dart VM when running on the command-line or as a server-side app. This does not work in a browser or when compiled to JavaScript.)

Setup

First, you need to import the dart:io library. This library contains the classes required to access files, directories, and more.

import 'dart:io';

Second, create a new instance of the Directory class.

var dir = new Directory('path/to/my/dir');

Listing contents in a script

The easiest way is to use the new listSync method. This returns a List of contents. By default this does not recurse.

List contents = dir.listSync();
for (var fileOrDir in contents) {
  if (fileOrDir is File) {
    print(fileOrDir.name);
  } else if (fileOrDir is Directory) {
    print(fileOrDir.path);
  }
}

If you want to recurse through directories, you can use the optional parameter recursive.

List allContents = dir.listSync(recursive: true);

WARNING if your directory structure has circular symlinks, the above code will crash because it's following symlinks recursively.

This method, using listSync, is especially useful when you are writing a shell script, command-line utility, or similar app or script with Dart.

Listing contents in a server

A second way to list the contents of a directory is to use the async version of list. You would use this second method when you need to list a directory in response to, say, an HTTP request. Remember that each of Dart's isolates runs in a single thread. Any long running process can block the event loop. When interactivity is important, or serving lots of clients from a single Dart script, use the async version.

With the async version, dir.list() returns a DirectoryLister. You can register three different callbacks on DirectoryLister:

  • onFile: called when a file or directory is encountered
  • onDone: called when the directory lister is done listing contents
  • onError: called when the lister encounters some error

Here is a simple function that returns a Future of a list of strings, containing file names in a directory:

Future<List<String>> dirContents(Directory dir) {
  var filenames = <String>[];
  var completer = new Completer();
  var lister = dir.list();
  lister.onFile = (filename) => filenames.add(filename);
  // should also register onError
  lister.onDone = (_) => completer.complete(filenames);
  return completer.future;
}

Of course, this method is perfect for servers, it's more cumbersome for simple scripts.

Luckily, Dart supports both methods for you to use!

Seth Ladd
  • 112,095
  • 66
  • 196
  • 279
6

Inside and asynchronous function write this

List<FileSystemEntity> allContents = await Directory("folder/path").list().toList();

Now you have a list with all of the contents

NewToPi
  • 187
  • 2
  • 9
  • 1
    This answer adds basically nothing to the question. Everything was written already in previous answers. – Magiczne Sep 30 '20 at 19:18
3

Here is my version using async/await, returning a List of Files only:

List<File> filesInDirectory(Directory dir) async {
  List<File> files = <File>[];
  await for (FileSystemEntity entity in dir.list(recursive: false, followLinks: false)) {
    FileSystemEntityType type = await FileSystemEntity.type(entity.path);
    if (type == FileSystemEntityType.FILE) {
      files.add(entity);
      print(entity.path);
    }
  }
  return files;
}
Jon K
  • 327
  • 2
  • 9
3

With this function you can print all the directories and files of a directory. You just need to pass a specific path.

Future listDir(String folderPath) async {
  var directory = new Directory(folderPath);
  print(directory);

  var exists = await directory.exists();
  if (exists) {
    print("exits");

    directory
      .list(recursive: true, followLinks: false)
      .listen((FileSystemEntity entity) {
        print(entity.path);
      });
  }
}
Seth Ladd
  • 112,095
  • 66
  • 196
  • 279
Ramses Aldama
  • 327
  • 1
  • 3
  • 11
  • Thanks for the corrections. This is more clear and readable. Is it necessary to add the **async** keyword? Is it not implicit when you make the function return type **Future**? – Ramses Aldama Apr 03 '17 at 09:33
  • The async keyword converts the declared field into a future so the return type of the function can be omitted. – Ramses Aldama Apr 05 '17 at 09:55
  • Thanks for this it really works like a charm. – Ahsan Dec 28 '21 at 05:03
1

To get a list of file names with a certain string, you can use this code:

String directory = (await getApplicationDocumentsDirectory()).path;
List<FileSystemEntity> files = Directory(directory).listSync(recursive: false);

List<String> filePaths = [];
for (var fileSystemEntity in files) {
  if (basename(fileSystemEntity.path).contains('mystring')) {
    filePaths.add(fileSystemEntity.path);
  }
}

You can use the basename function if you need just the file name, and not the whole path.

live-love
  • 48,840
  • 22
  • 240
  • 204
0

To get all files with given extension:

import 'package:path/path.dart' as p;

Future<List<File>> getAllFilesWithExtension(String path, String extension) async {
  final List<FileSystemEntity> entities = await Directory(path).list().toList();
  return entities.whereType<File>().where((element) => p.extension(element.path) == extension).toList();
}
jRicardo
  • 804
  • 10
  • 20