Skip to content

Instantly share code, notes, and snippets.

@thosakwe
Forked from tobischw/copy_dir.dart
Created July 17, 2019 15:54
Show Gist options
  • Save thosakwe/681056e86673e73c4710cfbdfd2523a8 to your computer and use it in GitHub Desktop.
Save thosakwe/681056e86673e73c4710cfbdfd2523a8 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 path;
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'));
@mi875
Copy link

mi875 commented Jul 27, 2021

I will use it.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment