copyDirectory function

Future<void> copyDirectory(
  1. Directory source,
  2. Directory destination
)

Copies all files and directories recursively from source to destination Only copies files that are newer or don't exist in target

Implementation

Future<void> copyDirectory(Directory source, Directory destination) async {
  if (!await destination.exists()) {
    await destination.create(recursive: true);
  }

  await for (final entity in source.list(recursive: false)) {
    final entityName = path.basename(entity.path);
    final destPath = path.join(destination.path, entityName);

    if (entity is Directory) {
      final newDir = Directory(destPath);
      await copyDirectory(entity, newDir);
    } else if (entity is File) {
      if (await shouldCopyFile(entity, destPath)) {
        await entity.copy(destPath);
        print('$entityName');
      } else {
        print('$entityName skipped (not modified)');
      }
    }
  }
}