Skip to content

Instantly share code, notes, and snippets.

@tobischw
Forked from thosakwe/copy_dir.dart
Created July 31, 2019 00:19
Show Gist options
  • Save tobischw/2965f2c0c6ed40703eb1fec3148a9fc8 to your computer and use it in GitHub Desktop.
Save tobischw/2965f2c0c6ed40703eb1fec3148a9fc8 to your computer and use it in GitHub Desktop.
Recursively copy directory in Dart (requires "path")
/*
* I'm sure there's a better way to do this, but this solution works for me.
* Recursively copies a directory + subdirectories into a target directory.
* There's also no error handling. Have fun.
*/
import 'dart:io';
import 'package:path/path.dart' as p;
Future<void> copyDirectory(Directory source, Directory destination) async {
await for (var entity in source.list(recursive: false)) {
if (entity is Directory) {
var newDirectory =
Directory(p.join(destination.absolute.path, p.basename(entity.path)));
await newDirectory.create();
await copyDirectory(entity.absolute, newDirectory);
} else if (entity is File) {
await entity.copy(p.join(destination.path, p.basename(entity.path)));
}
}
}
// HOW TO USE IT:
await copyDirectory(Directory('cool_pics/tests'), Directory('new_pics/copy/new'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment