copyDirectory static method

void copyDirectory(
  1. Directory source,
  2. Directory destination
)

Implementation

static void copyDirectory(Directory source, Directory destination) {
  if (!source.existsSync()) {
    throw Exception("Source directory doesn't exist: ${source.path}");
  }

  if (destination.existsSync()) {
    destination.deleteSync(recursive: true);
  }
  destination.createSync(recursive: true);
  source.listSync(recursive: false).forEach((FileSystemEntity entity) {
    if (entity is File) {
      entity.copySync('${destination.path}/${entity.uri.pathSegments.last}');
    } else if (entity is Directory) {
      copyDirectory(entity, Directory('${destination.path}/${entity.uri.pathSegments.last}'));
    }
  });
}