DirectoryScanner class final
Lists a directory on disk as TreeEntrys, ready for DirectoryTree.build.
Use a scanner when the paths come from the local file system rather than
from a picker, a server, or git ls-files. It walks the directory with an
explicit stack (no recursion, so very deep trees are safe), applies the
filters below while walking (a skipped directory is never listed), and
returns the entries sorted by path.
What scan and scanSync return:
- the scanned directory itself and every directory found below it, as
TreeEntry.directory(path, data: Directory(path)). Including the root keeps empty directories and makes the scanned directory the single top-level node of the tree that scanTree builds; - every file as
TreeEntry.file(path, data: File(path)). Other non-directory entities (pipes, sockets, devices) are reported the same way, with File data; - every symbolic link that is not followed as
TreeEntry.file(path, data: Link(path)): all links when followLinks is false, and with followLinks true the broken links and the links that would close a cycle.
Paths are exactly what dart:io reports: the scanned path as given,
followed by the platform separator and each name (so a relative input
gives relative entries, and a symbolic link keeps its own path, not its
target's). Only scanTree rewrites its input, and only for a relative
path that climbs above its start (see there). Entries are sorted by
String.compareTo on TreeEntry.path (UTF-16 code units), so the result
is the same on every run over an unchanged directory.
These paths use host syntax: \ is a separator on Windows but an
ordinary file-name character on Linux and macOS. scanTree parses them
accordingly. When you build a tree from the entries of scan or
scanSync yourself, do the same, because the default PathPolicy
treats \ as a separator everywhere and would split a POSIX name such
as w\z.txt into a directory and a file:
final tree = DirectoryTree.build(
await const DirectoryScanner().scan(path),
options: TreeOptions(
paths: PathPolicy(backslashIsSeparator: Platform.isWindows),
),
);
Filters are applied in this order to each entity below the root: includeHidden, then skip; maxDepth limits which directories are listed. The root itself is never filtered.
Per-directory failures (permission denied, a directory removed while the walk runs) never abort the scan: the directory stays in the result as an entry, its listing error goes to onError, and the walk continues. Only an invalid scan path or an invalid maxDepth throws.
On Windows, dart:io cannot list a directory whose path is longer than
the 260-character MAX_PATH limit (deep node_modules trees often
are), even where it can create one. When such a listing fails, the
scanner lists the directory again through its extended-length form
(\\?\C:\... or \\?\UNC\server\share\...) and reports the children
under the same spelling as the rest of the scan, so the walk reaches
every level. Only when that second listing fails too does its error go
to onError. With followLinks, a link at such a path is typed and
resolved the same way, and a resolved target that comes back in
extended-length form is compared in its ordinary form, so cycle
detection sees one spelling. The scan root gets the same retries, so a
root past MAX_PATH can be passed by its ordinary path; the reported
paths keep that spelling.
A small tree-like command line tool:
import 'dart:io';
import 'package:directory_tree/directory_tree.dart';
import 'package:directory_tree/io.dart';
Future<void> main(List<String> args) async {
final scanner = DirectoryScanner(
maxDepth: 3,
skip: (entity) => entity.path.endsWith('${Platform.pathSeparator}build'),
onError: (error) => stderr.writeln('skipped: ${error.path}'),
);
final tree = await scanner.scanTree(args.isEmpty ? '.' : args.first);
for (final node in tree.nodes) {
stdout.writeln('${' ' * node.depth}${node.label}');
}
}
For guide lines like the tree command, render the tree that scanTree
returns with the package's text renderer instead of indenting by hand.
- Annotations
-
- @immutable
Constructors
- DirectoryScanner({bool includeHidden = false, bool followLinks = false, int? maxDepth, bool skip(FileSystemEntity entity)?, void onError(FileSystemException error)?})
-
Creates a scanner; the scan path is given later to scan, scanSync,
or scanTree, so one scanner can be reused for many directories.
const
Properties
- followLinks → bool
-
Whether symbolic links are resolved.
final
- hashCode → int
-
The hash code for this object.
no setteroverride
- includeHidden → bool
-
Whether entities whose name starts with
.are included.final - maxDepth → int?
-
How many levels below the scanned directory are listed.
final
- onError → void Function(FileSystemException error)?
-
Receives the failures that the walk recovers from.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- skip → bool Function(FileSystemEntity entity)?
-
Decides which entities to leave out; return true to omit one.
final
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
scan(
String path) → Future< List< TreeEntry< >FileSystemEntity> > -
Lists the directory at
pathand everything below it, without blocking the event loop for long. -
scanSync(
String path) → List< TreeEntry< FileSystemEntity> > -
Lists the directory at
pathand everything below it synchronously. -
scanTree(
String path, {TreeOptions options = const TreeOptions()}) → Future< DirectoryTree< FileSystemEntity> > -
Scans
pathwith scan and builds a DirectoryTree from the entries. -
toString(
) → String -
A string representation of this object.
override
Operators
-
operator ==(
Object other) → bool -
Whether
otheris a DirectoryScanner with equal settings.override