retainOnly method

void retainOnly(
  1. Set<String> keep
)

Deletes every file of this kind not named in keep. A file another process holds stays; files of other names are never touched.

Implementation

void retainOnly(Set<String> keep) {
  final folder = Directory(dir);
  if (!folder.existsSync()) return;
  final name = RegExp('^([0-9a-f]{64})\\.${RegExp.escape(ext)}\$');
  for (final f in folder.listSync()) {
    if (f is! File) continue;
    final m = name.firstMatch(f.uri.pathSegments.last);
    if (m == null || keep.contains(m[1])) continue;
    try {
      f.deleteSync();
    } on FileSystemException {
      // In use elsewhere: it goes next time.
    }
  }
}